Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Loop through winform datagridview and count checkboxes that are checked
 

Loop through winform datagridview and count checkboxes that are checked

I'musingvb.net andhave a winform datagridview with a column of checkboxes. I'd like to loop through and count the boxes that are checked and put that value into a variable to be used elsewhere. Seems like this should be pretty straight forward but I can only find examples for ASP and C#.

Below is what I have so far. It stops working if I have more than 1 box checked...meaning, If I have no boxes checked it will return "0". If I have 1 box checked it will return "1". If I have 2 or more boxes checked it returns "1". Thanks in for the help.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim count = 0

Dim row As DataGridViewRow

For Each row In TblOrgPhoneDataGridView.Rows

If row.Cells(3).Value = True Then

count = +1

End If

Next

MessageBox.Show(count)

End Sub



TCD67  Tuesday, July 28, 2009 10:44 PM

the focus has to change from the checkbox cellfor the code's results to becorrect. I just moved the focus toa textbox on the form and it works like a champ.

Me.IdkOrganizationTextBox.Focus()
Dim pcount As Integer = 0
        For Each row As DataGridViewRow In TblOrgPhoneDataGridView.Rows
            If row.Cells(3).Value = True Then
                pcount += 1
            End If
        Next








  • Marked As Answer byTCD67 Wednesday, July 29, 2009 2:09 AM
  •  
TCD67  Wednesday, July 29, 2009 2:09 AM

Figured out my mistake right after posting...

count = +1

should be

count += 1



thanks all

TD

  • Unmarked As Answer byTCD67 Wednesday, July 29, 2009 12:37 AM
  • Marked As Answer byTCD67 Tuesday, July 28, 2009 10:51 PM
  •  
TCD67  Tuesday, July 28, 2009 10:51 PM
I spoke to soon. The code above will loop through the grid and count the checked boxes butthe resultslag behind as if the grid issome how notrefreshing. I need to checkand see how many check boxes are checked beforesending the changes to the database. I changed the code around a bit totryand target the checkboxcell, but it still does not work correctly.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click      
  Me.TblOrgPhoneDataGridView.Refresh()
        Dim count As Integer = 0
        For Each row As DataGridViewRow In TblOrgPhoneDataGridView.Rows
            Dim cb As DataGridViewCheckBoxCell = CType(row.Cells(3), DataGridViewCheckBoxCell)
            If cb.Value = True Then
                count += 1
            End If
        Next
         MessageBox.Show(count)
    End Sub
I suspect that I'm looking at the underlying value of the checkboxrather than the CheckState or something like that. Any helpwould be greatly appreciated. Thanks.

TD

TCD67  Wednesday, July 29, 2009 12:37 AM

the focus has to change from the checkbox cellfor the code's results to becorrect. I just moved the focus toa textbox on the form and it works like a champ.

Me.IdkOrganizationTextBox.Focus()
Dim pcount As Integer = 0
        For Each row As DataGridViewRow In TblOrgPhoneDataGridView.Rows
            If row.Cells(3).Value = True Then
                pcount += 1
            End If
        Next








  • Marked As Answer byTCD67 Wednesday, July 29, 2009 2:09 AM
  •  
TCD67  Wednesday, July 29, 2009 2:09 AM

You can use google to search for other answers

Custom Search

More Threads

• Override how GridView gets data from DataRow
• Report Viewer Control ?
• Datagridview and merged columnheaders
• Howto implement Form.IsDirty?
• How to avoid dragging a form
• Datagrid to Datatable
• read from access file on selected index
• Datagrid: Photo's
• loop unbound datagrid column
• ComboBox in DataGridView, setting the combo box's values accordingly to each row