0

我有一个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

哪一个是最好的?

4

1 回答 1

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
于 2013-08-12T10:19:21.367 回答