Selecting a row in DataGridView programmatically

C#.NetWinformsDatagridview

C# Problem Overview


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

C# Solutions


Solution 1 - C#

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;
}

Solution 2 - C#

In Visual Basic, do this to select a row in a DataGridView; the selected row will appear with a highlighted color but note that the cursor position will not change:

Grid.Rows(0).Selected = True

Do this change the position of the cursor:

Grid.CurrentCell = Grid.Rows(0).Cells(0)

Combining the lines above will position the cursor and select a row. This is the standard procedure for focusing and selecting a row in a DataGridView:

Grid.CurrentCell = Grid.Rows(0).Cells(0)
Grid.Rows(0).Selected = True

Solution 3 - C#

DataGridView.Rows
    .OfType<DataGridViewRow>()
     .Where(x => (int)x.Cells["Id"].Value == pId)
     .ToArray<DataGridViewRow>()[0]
     .Selected = true;

Solution 4 - C#

 <GridViewName>.ClearSelection(); ----------------------------------------------------1
 foreach(var item in itemList) -------------------------------------------------------2
 {
    rowHandle =<GridViewName>.LocateByValue("UniqueProperty_Name", item.unique_id );--3
    if (rowHandle != GridControl.InvalidRowHandle)------------------------------------4
    {
        <GridViewName>.SelectRow(rowHandle);------------------------------------ -----5
    }
  }
  1. Clear all previous selection.
  2. Loop through rows needed to be selected in your grid.
  3. Get their row handles from grid (Note here grid is already updated with new rows)
  4. Checking if the row handle is valid or not.
  5. When valid row handle then select it.

Where itemList is list of rows to be selected in the grid view.

Solution 5 - C#

Try This:

datagridview.Rows[currentRow].Cells[0];

Solution 6 - C#

You can use the Select method if you have a datasource: http://msdn.microsoft.com/en-us/library/b51xae2y%28v=vs.71%29.aspx

Or use linq if you have objects in you datasource

Solution 7 - C#

Try this:

DataGridViewRow row = dataGridView1.Rows[index row you want];
dataGridView1.CurrentRow = row;

Hope this help!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNagendraView Question on Stackoverflow
Solution 1 - C#Christoph FinkView Answer on Stackoverflow
Solution 2 - C#Rodrigo T.View Answer on Stackoverflow
Solution 3 - C#IM999MaxBonumView Answer on Stackoverflow
Solution 4 - C#IFlyHighView Answer on Stackoverflow
Solution 5 - C#user6314898View Answer on Stackoverflow
Solution 6 - C#thekipView Answer on Stackoverflow
Solution 7 - C#lecntpView Answer on Stackoverflow