Reputation: 1703
How can I select a particular range of rows in a DataGridView
programmatically at runtime?
Upvotes: 146
Views: 317175
Reputation: 3
Try this:
DataGridViewRow row = dataGridView1.Rows[index row you want];
dataGridView1.CurrentRow = row;
Hope this help!
Upvotes: -4
Reputation: 23113
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;
}
Upvotes: 187
Reputation: 181
DataGridView.Rows
.OfType<DataGridViewRow>()
.Where(x => (int)x.Cells["Id"].Value == pId)
.ToArray<DataGridViewRow>()[0]
.Selected = true;
Upvotes: 17
Reputation: 805
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
Upvotes: 53
Reputation: 546
<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
}
}
Where itemList is list of rows to be selected in the grid view.
Upvotes: 0
Reputation: 3768
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
Upvotes: -1