我在VB.NET类中有一个datagrid。细胞是可编辑的。有没有办法在编辑每个单元格时触发一个函数?
答案 0 :(得分: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
EDIt根据您的评论: 是的,您可以将当前值保存到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
答案 1 :(得分:1)
但CellValueChanged事件返回每个keydown动作的消息,即使你还没有完成输入,这个事实很烦人。