vb.net 如何检测VB.NET中数据网格的变化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13803127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to detect changes of data grid in VB.NET
提问by Geo Paul
I have a datagrid in VB.NET class. The cells are editable. Is there any way to trigger a function when each of the cells are edited?
我在 VB.NET 类中有一个数据网格。单元格是可编辑的。当编辑每个单元格时,有什么方法可以触发功能吗?
回答by Steve
Yes, the CellValueChanged Event: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvaluechanged.aspx
是的,CellValueChanged 事件:http: //msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvaluechanged.aspx
Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
'do something
End Sub
EDIt as per your comment: Yes you could save the current value to a class level variable on BeginEdit and retreieve it in the CellValueChanged event:
根据您的评论编辑:是的,您可以将当前值保存到 BeginEdit 上的类级别变量,并在 CellValueChanged 事件中检索它:
Private cellValue As String = String.Empty
Private Sub DataGridView1_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles DataGridView1.CellBeginEdit
cellValue = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString
End Sub
Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
MessageBox.Show("row index: " & e.RowIndex & Environment.NewLine & "old value: " & cellValue)
End Sub
回答by Steve
But CellValueChanged event return the message every keydown action even you havent completed to type, this fact is irritating.
但是 CellValueChanged 事件即使您还没有完成输入,也会在每个按键操作时返回消息,这个事实令人恼火。

