alwaysVBNET
alwaysVBNET

Reputation: 3310

Getting RowIndex based on the selected cells on a DataGridView

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

Answers (4)

alwaysVBNET
alwaysVBNET

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

user3025397
user3025397

Reputation: 71

DGV.CurrentRow.Index

Will work even if selectionMode = CellSelect

Upvotes: 7

Philip Stratford
Philip Stratford

Reputation: 4733

I don't think I'm understanding the question. Why does

iRowIndex = grdTransaction.SelectedRow.RowIndex

not work?

Upvotes: 3

matzone
matzone

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

Related Questions