1

I have a DataGridView which contains a DataGridViewColumn and also a button. When I click the button I want to check if all checkboxes in the datagridview are checked or not.

I use the following code but it is not working:

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
1
  • If Not CheckBox.Value = Not CheckBox.Value Then This will always evaluate true. Commented Jul 4, 2012 at 21:17

2 Answers 2

3

I think you have a problem with your IF statement. It should be checking if Value = True instead of .value = Not Checkbox,Value

If CheckBox.Value = True Then
   MsgBox("True")
End If
Sign up to request clarification or add additional context in comments.

Comments

0

I can't really tell what your logic is supposed to be doing with this line:

If Not CheckBox.Value = Not CheckBox.Value Then

It looks like you are saying, "if Not Value = Not Value" ???

Try this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.