Selecting a row in DataGridView programmatically
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Selecting a row in a WinForms DataGridView programmatically is simple once you know whether you want to select by index, by value, or by current cell. The important part is not just setting Selected = true, but also clearing previous selection when needed and making sure the row is visible to the user.
Select by Row Index
If you already know the zero-based row index, select it directly.
This does three useful things:
- clears existing selection
- marks the row as selected
- sets the current cell so keyboard focus and UI state are consistent
If you only set Selected = true, the visual result may not fully match user expectations.
Validate the Index First
Never assume the requested row exists.
This prevents runtime exceptions when the grid has fewer rows than expected.
Select a Row by Cell Value
Often the real problem is not "select row 3" but "select the row whose ID is 42."
This is usually the more robust pattern because row indices may change after sorting or filtering.
Scroll the Selected Row into View
If the row is off-screen, select it and make sure the grid scrolls to it.
That improves the user experience because selection is not hidden outside the current viewport.
Full Row Select Mode
Programmatic row selection behaves more naturally if the grid is configured for full-row selection.
Without that mode, users may still see cell-level selection behavior that does not match your row-oriented code.
Bound Data and Sorting
If the grid is bound to a data source and the user can sort columns, row indices are view positions, not stable record identities.
That means:
- selecting by index is fragile after sorting
- selecting by a key column is usually safer
In data-driven forms, prefer value-based lookup when possible.
If the data source changes frequently, you may also need to reapply selection after a refresh instead of assuming the old row object still represents the same business entity.
Multiple Selection Cases
If MultiSelect is enabled, you may intentionally want to select more than one row:
But for a single-row selection workflow, call ClearSelection() first and keep MultiSelect = false.
That makes selection logic easier to reason about.
Common Pitfalls
- Selecting a row without clearing prior selection in single-select workflows.
- Forgetting to set
CurrentCell, leaving focus behavior inconsistent. - Selecting by row index when sorting makes the index unstable.
- Failing to validate the index before accessing
Rows[rowIndex]. - Selecting an off-screen row without scrolling it into view.
Summary
- To select a row programmatically, set
Selected = trueand usually also setCurrentCell. - Clear existing selection first when only one row should remain selected.
- Select by key value instead of row index when the grid is sortable or data-bound.
- Validate row indices and scroll selected rows into view when needed.
- Configure full-row selection if the UX is meant to be row-oriented.

