我有一个datagridview,它的第一列是datagridviewcheckboxcolumn。 我想要的是检测用户何时检查/取消选中datagridview第一列中的任何复选框,以便我在两个解决方案中思考:
1)
Private Sub OnDgvCellClick(sender As Object, _
e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv.CellClick
If e.ColumnIndex = 0 Then
If Convert.ToBoolean(Me.dgv.Rows(e.RowIndex).Cells("chkcol").Value) = False then
' Do some stuff
else
' Do some stuff
End If
End If
End Sub
2)迭代所有datagridview行,并在第一列的所有复选框上为checkedchanged事件添加一个监听器。
Private Sub AddCheckedChangedListener()
For i As Integer = 0 To Me.dgv.RowCount - 1
AddHandler CType(Me.dgvDatos(0, i).Value, CheckBox).CheckedChanged, AddressOf OnCheckBoxCheckedChanged
Next
End Sub
哪一个最好?
答案 0 :(得分:0)
试试这个
Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
End Sub
Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
If e.RowIndex < 0 Or e.ColumnIndex < 0 Then Exit Sub
MsgBox(DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value)
End Sub