如何获取DataGridViewCheckBoxColumn的当前选择状态?
VB2005
在DataGridView加入一列DataGridViewCheckBoxColumn,请问如可正确获取当前被点击的CheckBox的Checked或UnChecked状态?
本人常试用DataGridView1.Item(e.ColumnIndex, e.RowIndex).Value方法,但每次都返回同一个值(可能是由于当前的CheckBox处于编辑状态,所以Check值未能更新)。
Public Class Form1
Dim columnCheckBox1 As New System.Windows.Forms.DataGridViewCheckBoxColumn
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DataGridView1.Columns.Add(columnCheckBox1)
DataGridView1.ReadOnly = False
DataGridView1.Rows.Add()
End Sub
Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
MsgBox(DataGridView1.Item(e.ColumnIndex, e.RowIndex).Value) '每次都是返回一样的值
End Sub
End Class
[解决办法]
因此,你可以按如下的代码改写你的程序:
Private Sub dataGridView1_CellClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)
If Not Object.Equals(Me.dataGridView1.Item(e.ColumnIndex, e.RowIndex).Value, True) Then
Me.dataGridView1.Item(e.ColumnIndex, e.RowIndex).Value = True
Else
Me.dataGridView1.Item(e.ColumnIndex, e.RowIndex).Value = False
End If
Console.WriteLine(Me.dataGridView1.Item(e.ColumnIndex, e.RowIndex).Value)
End Sub