How to get the checked columns in DataGridView

  • Thread starter Thread starter Annie
  • Start date Start date
A

Annie

hello guys,

I am having a DataGridView control.

The first column is a checkbox column.

The users can select the checkbox column. I need to loop through the grid
rows and get
the checked checkboxes and the rows numbers

I don't know how to check if the first column checkbox is checked???

any help will be appreciated!
 
Annie,

How did you populate your DataGridView, that can in thousand and one ways.

Cor
 
Hi Annie,

you could try something like this , just susbstitute the name of your
datagridview and the name or index of the cell that contains the checkbox.

For n As Integer = 0 To Grid.Rows.Count - 1
If CBool(YourGrid.Rows(n).Cells("Selected").Value) = True Then
Return True
End If
Next

HTH,

Martin
 
Just re-read your post, this should be closer to what you are after. Or you
could use a list of datagridviewrows and return that instead.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
For Each num As Integer In SelectedRows()
Debug.Print(num.ToString)
Next
End Sub

Private Function SelectedRows() As List(Of Integer)
Dim list As New List(Of Integer)
Dim Grid As DataGridView = Me.InvoicesControl1.InvoicesDataGridView

For n As Integer = 0 To Grid.Rows.Count - 1
If CBool(Grid.Rows(n).Cells("Selected").Value) = True Then
list.Add(n)
End If
Next
Return list
End Function

HTH,

Martin.
 
Back
Top