How can I select a particular range of rows in a DataGridView
programmatically at runtime?
Asked
Active
Viewed 2.9e+01k times
141

Blorgbeard
- 101,031
- 48
- 228
- 272

Nagendra
- 1,645
- 4
- 12
- 14
7 Answers
175
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;
}

Philippe
- 28,207
- 6
- 54
- 78

Christoph Fink
- 22,727
- 9
- 68
- 113
-
2Beware if your DataGridView is `read-only` - then `SelectedRows.Clear()` will not work. – Derek W Sep 09 '13 at 13:05
-
20Instead of SelectedRows.Clear() use the ClearSelection() method of the datagridview instance – Furkan May 18 '14 at 12:40
-
2@Furkan: That does not do the same thing. With `SelectedRows.Clear()` only the selected rows are cleared, but with `ClearSelection()` also the columns => column-selections are lost... – Christoph Fink May 18 '14 at 12:45
-
26add one line of code `dataGrid.CurrentCell = dataGrid.Rows[row.Index].Cells[0];` – Timeless Sep 18 '15 at 09:56
-
2@Timeless: the SelectionChanged event does not seem to be triggered when testing your suggestion. – user2430797 Mar 15 '18 at 02:29
-
@user2430797 I suspect we only set current cell `.CurrentCell` but not current row. if you want to trigger `SelectionChanged`, maybe you want to try `.Rows[index].Selected = true;` **note: not tested.** – Timeless Mar 15 '18 at 03:25
-
`foreach (DataGridViewRow item in dataGridView1.Rows) { if (((int)item.Cells["ImageWidth"].Value > 1000 || (int)item.Cells["ImageHeigth"].Value > 1000) && (bool)item.Cells["Exists"].Value == false) { item.Selected = true; dataGridView1.Rows[item.Index].Selected = true; label3.Text = label3.Text + " " + item.Index.ToString(); } } ` none of my records in the dataview gets selected, label3 does have the correct indexes which needs to be selected... – Luuk Mar 17 '19 at 15:52
-
comments with nice formatting seems to be a problem ;) – Luuk Mar 17 '19 at 15:54
49
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
-
Grid.Rows(0).Cells(0) is very useful in case of grid data longer than the grid display area. The order mentioned above is not necessary, you can swipe the code for row/cell selection [ source: Personally Tested ]. – BiLaL Dec 27 '14 at 15:13
-
How to move the grid to selected position? Row is selected but grid position is not on selected row. Manually scrolling shows the selected row. How to force the grid to scroll? – FrenkyB Jul 21 '17 at 11:35
-
2
-
Take care that whatever column you reference in `.Cells(x)` is `Columns(x).Visible = True` or it will error. Use [DataGridView.FirstDisplayed](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.datagridview.firstdisplayedcell?redirectedfrom=MSDN&view=net-5.0#System_Windows_Forms_DataGridView_FirstDisplayedCell) to find the first one. – ourmandave Jul 01 '21 at 16:33
16
DataGridView.Rows
.OfType<DataGridViewRow>()
.Where(x => (int)x.Cells["Id"].Value == pId)
.ToArray<DataGridViewRow>()[0]
.Selected = true;

shA.t
- 16,580
- 5
- 54
- 111

IM999MaxBonum
- 171
- 1
- 5
0
<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
}
}
- Clear all previous selection.
- Loop through rows needed to be selected in your grid.
- Get their row handles from grid (Note here grid is already updated with new rows)
- Checking if the row handle is valid or not.
- When valid row handle then select it.
Where itemList is list of rows to be selected in the grid view.

IFlyHigh
- 546
- 2
- 9
- 20
0
Try This:
datagridview.Rows[currentRow].Cells[0];

Ganesh Kamath - 'Code Frenzy'
- 5,094
- 3
- 43
- 60
-
1[`datagridview.Rows[currentRow].Cells[0]`](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.datagridviewcellcollection.item?view=netcore-3.1#System_Windows_Forms_DataGridViewCellCollection_Item_System_Int32_) *Gets ... the cell at the provided index location.* How does this **select a particular range of rows** as requested in the question? – dbc Jun 05 '20 at 16:54
-1
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

thekip
- 3,660
- 2
- 21
- 41