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 in
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
}
Use the Index property in your DGV's SelectedRows collection:
int index = yourDGV.SelectedRows[0].Index;
I modified @JayRiggs' answer, and this works. You need the if
because sometimes the SelectedRows may be empty, so the index operation will throw a exception.
if (yourDGV.SelectedRows.Count>0){
int index = yourDGV.SelectedRows[0].Index;
}
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.
Try it:
int rc=dgvDataRc.CurrentCell.RowIndex;** //for find the row index number
MessageBox.Show("Current Row Index is = " + rc.ToString());
I hope it will help you.
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 )