Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a problem refreshing a DataGridView control after Insert or Update. The source code:
VB
Dim dt1 as DataTable = GetData("SELECT * FROM CLAIMSTATE ") 'return DataTable
dataGrid.DataSource = dt1
'On Event RowLeave I Insert or Update table from database
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 & "" = "" 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)

  dt1 = GetData("SELECT * FROM CLAIMSTATE ")
  'at this record I obtain this error: "operation cannot be performed in this event handler"
  dataGrid.DataSource = dt1
End Sub

I can reset the DataSource in this way:
VB
dataGrid.Enabled = False
dataGrid.DataSource = Null
dataGrid.Enabled = False

I found this page:
http://stackoverflow.com/questions/2760657/failure-to-validate-but-cannot-remove-in-datagridview
but I don'n know how to write thet in VB.NET:
C#
BeginInvoke(new Action(delegate { dataGrid.DataSource = dt1; }));


Can anybody help me with conversion or with refresh the DataGridView or some ideas for resolve this problem?

Thank's in advance!
Posted

Have a read of this MSDN article it has VB.NET examples

MSDN: Control.BeginInvoke[^]
 
Share this answer
 
C#
dataGrid.Refresh();
 
Share this answer
 
Comments
wertyk 1-Feb-12 10:27am    
Doesn't work because I don't have a binding with table, I have only Select from table!
ZurdoDev 1-Feb-12 10:32am    
I guess we need more information. You should set the DataSource and call DataBind. If the DataSource is already set you can either call DataBind again or just call Refresh(). I believe we need more info to understand better.
wertyk 1-Feb-12 10:37am    
I can see the method GetData:

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
wertyk 1-Feb-12 10:41am    
I fill the gridview with the DataTable who return the method GetData and when I do Update and Insert in event RowLeave but I can't refresh the grid after Insert\Update.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900