2012-07-04 179 views
1

我有一个DataGridView其中包含一个DataGridViewColumn,也是一个按钮。当我点击按钮时,我想检查是否选中了datagridview中的所有复选框。检查是否选中复选框DataGridViewCheckBoxCell

我用下面的代码,但它不工作:

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 
+0

'如果不CheckBox.Value =不CheckBox.Value Then'这将始终评估真实。 –

回答

0

我真的不能告诉你的逻辑应该是这一行做:

如果不CheckBox.Value =不CheckBox.Value然后

它看起来像你说的,“如果不值=不值”???

试试这个:

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 
3

我认为你有一个问题,您的IF语句。应该检查是否的Value = True代替.value = Not Checkbox,Value

If CheckBox.Value = True Then 
    MsgBox("True") 
End If 
相关问题