title: How to refresh a dataGridView
body: <p>I have a problem refreshing a DataGridView control after <code>Insert</code> or <code>Update</code>. The source code:</p>
  
  <p>Get all rows from table in datatable and set to the datasource:</p>
  
  <pre><code>Dim dt1 as DataTable = GetData("SELECT * FROM CLAIMSTATE ")
  dataGrid.DataSource = dt1
  </code></pre>
  
  <p>Update Event if ID is valued, and Insert if it isn't:</p>
  
  <pre><code>Private Sub dataGrid_RowLeave(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dataGrid.RowLeave
  
  Dim row As DataGridViewRow = CType(sender, DataGridView).Rows(e.RowIndex)
    Dim query As New StringBuilder("")
    If row.Cells(0).Value &amp; "" = "" Then
      query.Append("INSERT INTO CLAIMSTATE ")
      query.Append("(CST_CODE, CST_LABEL, CST_POINTS)")
      query.Append("VALUES ")
      query.Append("(?, ?, ?)")
    Else
      query.Append("Update CLAIMSTATE ")
      query.Append("SET CST_CODE = ?, ")
      query.Append("CST_LABEL = ?, ")
      query.Append("CST_POINTS = ? ")
      query.Append("WHERE CST_ID = ? ")
    End If
    Dim command As New OdbcCommand(query.ToString(), con)
    command.Parameters.Add("@cst_code", OdbcType.Char).Value = row.Cells(1).Value
    command.Parameters.Add("@cst_label", OdbcType.NVarChar).Value = row.Cells(2).Value
    command.Parameters.Add("@cst_points", OdbcType.Decimal).Value = row.Cells(3).Value
    command.Parameters.Add("@cst_id", OdbcType.BigInt).Value = row.Cells(0).Value
  
    Dim res As Integer = ExecuteNonQuery(command)
  End Sub
  
  Public Function GetData(ByRef sqlQuery As String) As DataTable
      Dim command As New OdbcCommand(sqlQuery, con)
      Try
        If con.State = ConnectionState.Closed Then
          con.ConnectionString = conString
          con.Open()
        End If
        Using dr As OdbcDataReader = command.ExecuteReader()
          Dim dt As New DataTable()
          dt.Load(dr)
          Return dt
        End Using
        'con.Close()
      Catch ex As Exception
        MsgBox(ex.Message)
        con.Close()
        Return Null
      End Try
    End Function
  
  Public Function ExecuteNonQuery(ByRef command As OdbcCommand) As Integer
  Dim result As Integer = 0
  If con.State = ConnectionState.Closed Then
    con.ConnectionString = conString
    con.Open()
  End If
  'Dim command As New OdbcCommand(sqlQuery, conn)
  Try
    'command.Connection = con
    'Dim cmd As New OdbcCommand( sqlQuery, conn)
    result = command.ExecuteNonQuery()
  Catch
    result = 0
    If con IsNot Nothing Then
      con.Close()
      command.Dispose()
    End If
  Finally
    command.Dispose()
  End Try
  Return result
  End Function
  </code></pre>
  
  <p>I tried to get all records from the table and set the datasource again at the end of the method but it doesn't work.</p>
  
  <p>If I put the code:</p>
  
  <pre><code>dataGrid.Rows.Clear()
  dataGrid.Columns.Clear()
  dt1 = GetData("SELECT * FROM CLAIMSTATE ")
  dataGrid.DataSource = dt1
  </code></pre>
  
  <p>on end of event method RowLeave I recive this error: </p>
  
  <blockquote>
    <p>"Operation is not valid because it results in a reentrant call to the
    SetCurrentCellAddressCore function"</p>
  </blockquote>
  
  <p>on dataGrid.Rows.Clear(), but if  I remove the line codes Rows.Clear() and Columns.Clear(), the debug cursor after execute dataGrid.DataSource = dt1 return to begin of event method an execute some code again and after I recive the some error "...reentrant call to the SetCurrentCellAddressCore function"!</p>
  
  <p>Help me, please!</p>