Freelancer's Playground! New knowledges every now and then

29May/070

Get Selected Row from DataGridView

This is how to get selected row from DataGridView in .NET 2.0

  1. Make sure there's a single cell that is currently selected
  2. Get current row index using {datagridview instance}.CurrentCell.RowIdex
  3. Get the selected row using {datagridview instance}.Rows({current row index}).Cells({zero based index}).Value


Here's the sample code :

Dim crow = DataGridView1.CurrentCell.RowIndex
dim txt as string
Try
    txt = DataGridView1.Rows(crow).Cells(0).Value & " || "
    txt = txt & DataGridView1.Rows(crow).Cells(1).Value & " || "
    txt = txt & DataGridView1.Rows(crow).Cells(2).Value

    MessageBox.show (txt)
Catch ex As Exception
    ' do nothing
End Try