Selecting a program in a DataGridView programmatically

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

+86
c # winforms datagridview
Jun 07 2018-11-12T00:
source share
6 answers

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

 dataGrid.Rows[index].Selected = true; 

or you can do the following (but again: not verified):

 dataGrid.SelectedRows.Clear(); foreach(DataGridViewRow row in dataGrid.Rows) { if(YOUR CONDITION) row.Selected = true; } 
+99
Jun 07 2018-11-12T00:
source share

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

 Grid.Rows(0).Selected = True 

Change the cursor position:

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

The concatenation of the lines above will position the cursor and select the line. 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 
+24
Nov 14 '13 at 20:39
source share
 DataGridView.Rows .OfType<DataGridViewRow>() .Where(x => (int)x.Cells["Id"].Value == pId) .ToArray<DataGridViewRow>()[0] .Selected = true; 
+11
Sep 14 '13 at 14:03
source share

Try the following:

 datagridview.Rows[currentRow].Cells[0]; 
+2
May 26 '16 at
source share

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

Or use linq if you have objects in your data source

0
Jun 07 2018-11-12T00:
source share
  <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.
  • Scrolling lines should be selected in your grid.
  • Get your row handles from the grid (note that the grid is already updated with new rows)
  • Validation of the string descriptor.
  • When a valid line descriptor then select it.

If itemList is a list of rows that should be selected as a grid.

0
Oct 06 '14 at 12:18
source share



All Articles