Index of Currently Selected Row in DataGridView
It's that simple. How do I get the index of the currently selected Row
of a DataGridView
? I don't want the Row
object, I want the index (0 .. n).
Solution 1:
There is the RowIndex
property for the CurrentCell
property for the DataGridView.
datagridview.CurrentCell.RowIndex
Handle the SelectionChanged
event and find the index of the selected row as above.
Solution 2:
Use the Index property in your DGV's SelectedRows collection:
int index = yourDGV.SelectedRows[0].Index;
Solution 3:
dataGridView1.SelectedRows[0].Index;
Or if you wanted to use LINQ and get the index of all selected rows, you could do:
dataGridView1.SelectedRows.Select(r => r.Index);