I have a problem refreshing a DataGridView control after Insert or Update. The source code:
Dim dt1 as DataTable = GetData("SELECT * FROM CLAIMSTATE ")
dataGrid.DataSource = dt1
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 ")
dataGrid.DataSource = dt1
End Sub
I can reset the DataSource in this way:
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:
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!