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).
104
votes
12 Answers
179
votes
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.
38
votes
8
votes
6
votes
3
votes
2
votes
2
votes
1
votes
Try DataGridView.CurrentCellAddress.
Returns: A Point that represents the row and column indexes of the currently active cell.
E.G. Select the first column and the fifth row, and you'll get back: Point( X=1, Y=5 )
1
votes
0
votes
0
votes
I used if get row value is clicked:
private void dataGridView_Product_CellClick(object sender, DataGridViewCellEventArgs e){
int rowIndex;
//rowIndex = e.RowIndex; //Option 1
//rowIndex= dataGridView_Product.CurrentCell.RowIndex; //Option 2
rowIndex = dataGridView_Product.CurrentRow.Index; //Option 3
}