Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 883
alwaysVBNET Avatar asked Jun 18 '13 13:06

alwaysVBNET


People also ask

How to get selected item in DataGridView?

To get the selected cells in a DataGridView controlUse the SelectedCells property.


2 Answers

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
like image 179
alwaysVBNET Avatar answered Oct 01 '22 12:10

alwaysVBNET


DGV.CurrentRow.Index

Will work even if selectionMode = CellSelect

like image 45
user3025397 Avatar answered Oct 01 '22 13:10

user3025397