0

我在 VB.NET 类中有一个数据网格。单元格是可编辑的。编辑每个单元格时,有什么方法可以触发功能吗?

4

2 回答 2

3

是的,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

根据您的评论编辑:是的,您可以将当前值保存到 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
于 2012-12-10T14:43:50.153 回答
1

但是即使您还没有完成输入,CellValueChanged 事件也会在每次按键操作时返回消息,这个事实很烦人。

于 2013-04-29T10:15:56.390 回答