Reputation: 3310
I'm trying to get the row indices based on my selected cells on a DataGridView. How can I do that in VB.NET?
This is what I have:
Dim iRowIndex As Integer
For i = 0 To Me.grdTransaction.SelectedCells.Item(iRowIndex)
iRowIndex = Me.grdTransaction.SelectedCells.Item(i).RowIndex.ToString()
Dim s As String = Me.grdTransaction.SelectedRows(i).Cells("DataGridViewTextBoxColumn6").Value
aList.Add(s)
MsgBox("Row index " & iRowIndex)
Next
Upvotes: 5
Views: 95225
Reputation: 3310
Thanks to @matzone I have figured it out:
Dim iRowIndex As Integer
For i As Integer = 0 To Me.grdTransaction.SelectedCells.Count - 1
iRowIndex = Me.grdTransaction.SelectedCells.Item(i).RowIndex
aList.Add(Me.grdTransaction.Rows(iRowIndex).Cells("DataGridViewTextBoxColumn6").Value)
MsgBox("Row index " & Format(iRowIndex))
Next
Upvotes: 11
Reputation: 71
DGV.CurrentRow.Index
Will work even if selectionMode = CellSelect
Upvotes: 7
Reputation: 4733
I don't think I'm understanding the question. Why does
iRowIndex = grdTransaction.SelectedRow.RowIndex
not work?
Upvotes: 3
Reputation: 5719
You can try this ..
Dim iRowIndex As Integer
Dim s As String
For i as Integer = 0 To Me.grdTransaction.SelectedCells.Count -1
iRowIndex = Me.grdTransaction.SelectedCells.Item(i).RowIndex.ToString()
aList.Add(Me.grdTransaction.SelectedRows(i).Cells("DataGridViewTextBoxColumn6").Value)
MsgBox("Row index " & format(iRowIndex))
Next
Upvotes: 0