DataGridView
Programmatic Selection
C# Tutorial
WinForms
Row Selection

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.

csharp
1int rowIndex = 3;
2
3dataGridView1.ClearSelection();
4dataGridView1.Rows[rowIndex].Selected = true;
5dataGridView1.CurrentCell = dataGridView1.Rows[rowIndex].Cells[0];

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.

csharp
1int rowIndex = 3;
2
3if (rowIndex >= 0 && rowIndex < dataGridView1.Rows.Count)
4{
5    dataGridView1.ClearSelection();
6    dataGridView1.Rows[rowIndex].Selected = true;
7    dataGridView1.CurrentCell = dataGridView1.Rows[rowIndex].Cells[0];
8}

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."

csharp
1string targetId = "42";
2
3foreach (DataGridViewRow row in dataGridView1.Rows)
4{
5    if (row.Cells["IdColumn"].Value?.ToString() == targetId)
6    {
7        dataGridView1.ClearSelection();
8        row.Selected = true;
9        dataGridView1.CurrentCell = row.Cells[0];
10        break;
11    }
12}

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.

csharp
1int rowIndex = 25;
2
3if (rowIndex >= 0 && rowIndex < dataGridView1.Rows.Count)
4{
5    dataGridView1.ClearSelection();
6    dataGridView1.Rows[rowIndex].Selected = true;
7    dataGridView1.CurrentCell = dataGridView1.Rows[rowIndex].Cells[0];
8    dataGridView1.FirstDisplayedScrollingRowIndex = rowIndex;
9}

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.

csharp
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridView1.MultiSelect = false;

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:

csharp
dataGridView1.Rows[1].Selected = true;
dataGridView1.Rows[3].Selected = true;

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 = true and usually also set CurrentCell.
  • 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.

Course illustration
Course illustration