J2Tuner J2Tuner - 1 year ago 129
Vb.net Question

DataGridView Multiple Row Selection Issue

I am trying to check for multiple selections in a DataGridView with a For... Next Loop, but even though I have selected multiple rows, the only row with the property Selected=True is the first row in the selection. Is there a way around this?

MultiSelect is true on the DataGridView.

My code is as follows:

For Each dr As DataGridViewRow In dgv.Rows
If dr.Selected = True Then
intSelectedRow = dr.Index
SetTime("KeyEntry", dgv.Name, intSelectedRow)
End If


Answer Source

Try this:

Dim selectedItems As DataGridViewSelectedRowCollection = dgv.SelectedRows
      For Each selectedItem As DataGridViewRow In selectedItems
            'Add code to handle whatever you want for each row
End Sub