How to get the checked columns in DataGridView

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!
 
C

Cor Ligthert [MVP]

Annie,

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

Cor
 
M

Martin

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
 
M

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top