如何在DataGridView中刷新数据源 [英] How to refresh DataSource in DataGridView
本文介绍了如何在DataGridView中刷新数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在插入或更新后刷新DataGridView控件时遇到问题.源代码:
I have a problem refreshing a DataGridView control after Insert or Update. The source code:
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:
dataGrid.Enabled = False
dataGrid.DataSource = Null
dataGrid.Enabled = False
我找到此页面:
http://stackoverflow.com/questions/2760657/failure-to-验证但无法删除datagridview
但是我不知道如何在VB.NET中编写它们:
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; }));
有人可以帮助我进行转换或刷新DataGridView或解决此问题的一些想法吗?
Can anybody help me with conversion or with refresh the DataGridView or some ideas for resolve this problem?
Thank''s in advance!
推荐答案
已阅读此MSDN文章,其中包含VB.NET示例.
MSDN:Control.BeginInvoke [
Have a read of this MSDN article it has VB.NET examples
MSDN: Control.BeginInvoke[^]
dataGrid.Refresh();
这篇关于如何在DataGridView中刷新数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文