C# 检查DataGridViewCheckBoxCell中是否选中复选框
我有一个C# 检查DataGridViewCheckBoxCell中是否选中复选框,c#,vb.net,datagridview,C#,Vb.net,Datagridview,我有一个DataGridView,其中包含一个DataGridViewColumn和一个按钮。单击该按钮时,我想检查datagridview中的所有复选框是否都已选中 我使用以下代码,但它不起作用: Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click For i As Integer = 0 To DataGridView1.
DataGridView
,其中包含一个DataGridViewColumn
和一个按钮。单击该按钮时,我想检查datagridview中的所有复选框是否都已选中
我使用以下代码,但它不起作用:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
For i As Integer = 0 To DataGridView1.Rows.Count - 1
Dim CheckBox As DataGridViewCheckBoxCell = DirectCast(DataGridView1.Rows(i).Cells(0), DataGridViewCheckBoxCell)
If Not CheckBox.Value = Not CheckBox.Value Then
MsgBox("True")
End If
Next
End Sub
我认为你的IF声明有问题。它应该检查
Value=True
而不是。Value=Not复选框,Value
If CheckBox.Value = True Then
MsgBox("True")
End If
我真的不知道你的逻辑应该怎么处理这句话: 如果未选中复选框。值=未选中复选框。值,则 看起来你在说,“如果没有价值=没有价值” 试试这个:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
For i As Integer = 0 To DataGridView1.Rows.Count - 1
'Dim CheckBox As DataGridViewCheckBoxCell = DirectCast(DataGridView1.Rows(i).Cells(0), DataGridViewCheckBoxCell)
'If Not CheckBox.Value = Not CheckBox.Value Then
' MsgBox("True")
'End If
Dim obj As Object = DataGridView1.Rows(i).Cells(0)
If (Not obj Is Nothing) Then
Dim checkBox1 As DataGridViewCheckBoxCell = DirectCast(obj, DataGridViewCheckBoxCell)
Dim objValue As Object = checkBox1.Value
If (Not objValue Is Nothing) Then
Dim checked As Boolean = DirectCast(objValue, Boolean)
If (checked) Then
MsgBox("True")
End If
End If
End If
Next
End Sub
如果未选中复选框。值=未选中复选框。值则
这将始终计算为true。