Selecting a row in a DataGridView and having the arrow on the row header follow

BrianK picture BrianK · Jul 16, 2009 · Viewed 43.5k times · Source

This is in C#. If I select a row in a DataGridView with DataGridViewRow.Selected = true, the row selects just fine, but the arrow in the "column header" (the grey very leftmost column) doesn't follow. How do I set that?

Another question: If I set a column format to "centered" in the designer, the column header is still left aligned. How do I set the column header to be centered too?

Thanks, Brian

Answer

Kredns picture Kredns · Jul 16, 2009

This is straight from google:

In a DataGridView, the selected row and the current row (indicated by an arrow in the row header) may not be the same row. In addition, we could select multiple rows in a DataGridView but the current row can only be one row. When the SelectionMode property of the DataGridView is set to FullRowSelect, the current row will be always selected. If you'd like to change the current row in a DataGridView control, you may set the CurrentCell property
dataGridView1.CurrentCell = dataGridView1.Rows[1].Cells[0];
If you'd like to just change the selected row, you may set the Selected property of the row you want to true.
dataGridView1.CurrentRow.Selected = false;
dataGridView1.Rows[1].Selected = true;