C# – Selecting a row in DataGridView programmatically

cdatagridviewnetwinforms

How can I select a particular range of rows in a DataGridView programmatically at runtime?

Best Answer

Not tested, but I think you can do the following:

dataGrid.Rows[index].Selected = true;

or you could do the following (but again: not tested):

dataGrid.SelectedRows.Clear();
foreach(DataGridViewRow row in dataGrid.Rows)
{
    if(YOUR CONDITION)
       row.Selected = true;
}