DGV中复选框更改的最佳事件

时间:2015-08-06 19:09:12

标签: vb.net events checkbox datagridview

我在Microsoft Visual Studio Express 2013中使用SQL后端。我在DataGridView中有一个复选框列,我遇到问题,当我更改列中复选框的检查状态时,实际代码在我离开单元格之前不会运行。我尝试使用脏单元格状态更改,但一旦我这样做,我的代码不再识别e.rowindex或e.columnindex。是否有一个我可以使用的事件,只要切换复选框就会运行代码,并且仍会识别e.rowindex和e.columnindex代码?

 Private Sub DGVCutList_CurrentCellChanged(sender As Object, e As EventArgs) Handles DGVCutList.CurrentCellChanged
    If e.RowIndex = -1 Then
            Exit Sub
        End If
end sub

1 个答案:

答案 0 :(得分:0)

有一些不同的事件可用于实现此目的。要使用的两个事件是:CurrentCellDirtyStateChangedCellValueChanged。请参阅下面的代码段。

 'This will fire off after CurrentCellDirtyStateChanged occured...
 'You can get row or column index from e as well here...
 Private Sub DGVCutList_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DGVCutList.CellValueChanged
   'Do what you need to do...
 End Sub

 'This will fire immediately when you click in the cell...
 Private Sub DGVCutList_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles DGVCutList.CurrentCellDirtyStateChanged
  If DGVCutList.IsCurrentCellDirty Then
    DGVCutList.CommitEdit(DataGridViewDataErrorContexts.Commit)
  End If
 End Sub