Index of Currently Selected Row in DataGridView
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Windows Forms, the DataGridView control provides several ways to get the index of the currently selected row. The most common approaches are CurrentCell.RowIndex (returns the row of the active cell), SelectedRows[0].Index (returns the first selected row when full-row selection is enabled), and CurrentRow.Index (returns the row containing the current cell). The right property depends on your selection mode and whether you need to handle multi-row selection.
Using CurrentCell.RowIndex
CurrentCell always points to the cell that has focus, regardless of the selection mode. This works even when SelectionMode is set to CellSelect.
Using SelectedRows
SelectedRows is populated only when SelectionMode is FullRowSelect or RowHeaderSelect. With CellSelect, the collection is empty even when cells are selected.
Using CurrentRow
CurrentRow returns the DataGridViewRow that contains CurrentCell. It is null only when the grid has no rows.
Handling Multiple Selected Rows
Note that SelectedRows returns rows in the order they were selected (last selected first), not in display order. Sort explicitly if order matters.
Event-Based Row Selection
Getting Row Data from the Underlying DataSource
Comparison of Approaches
| Property | Returns | Selection Mode | Null When |
CurrentCell.RowIndex | Active cell's row | Any | No current cell |
SelectedRows[0].Index | First selected row | FullRowSelect / RowHeaderSelect | No rows selected |
CurrentRow.Index | Row of current cell | Any | No rows in grid |
SelectedCells[0].RowIndex | First selected cell's row | Any | No cells selected |
Common Pitfalls
- Accessing
SelectedRowswithCellSelectmode:SelectedRowsis empty whenSelectionModeisCellSelect. Switch toFullRowSelector useCurrentCell.RowIndexinstead. - Not checking for null before accessing properties:
CurrentCell,CurrentRow, andSelectedRows[0]can be null or out of bounds when the grid is empty or nothing is selected. Always check!= nullor.Count > 0before accessing. - Assuming
SelectedRowsorder matches display order:SelectedRowsreturns rows in reverse selection order (most recently selected first). Sort by.Indexif you need display order. - Using
SelectedRowsafter deleting rows: After removing a row, theSelectedRowscollection updates but indices shift. Re-query the selection after any modification to the grid's data source. - Clicking column headers triggering row selection: The
CellClickevent fires withe.RowIndex == -1when a column header is clicked. Always guard withif (e.RowIndex >= 0)to avoidArgumentOutOfRangeException.
Summary
- Use
CurrentCell.RowIndexfor the active cell's row — works with any selection mode - Use
SelectedRows[0].Indexfor full-row selection — requiresFullRowSelectmode - Use
CurrentRow.Indexfor the row containing the current cell - Always null-check before accessing selection properties
- Handle
SelectionChangedevent for real-time row tracking - Access the underlying data with
CurrentRow.DataBoundItemfor typed access to bound objects

