Selecting a row in DataGridView programmatically

后端 未结 8 2323
被撕碎了的回忆
被撕碎了的回忆 2020-12-08 12:58

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

相关标签:
8条回答
  • 2020-12-08 13:00

    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;
    }
    
    0 讨论(0)
  • 2020-12-08 13:07
    DataGridView.Rows
        .OfType<DataGridViewRow>()
         .Where(x => (int)x.Cells["Id"].Value == pId)
         .ToArray<DataGridViewRow>()[0]
         .Selected = true;
    
    0 讨论(0)
  • 2020-12-08 13:11

    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

    0 讨论(0)
  • 2020-12-08 13:13

    Try this:

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

    Hope this help!

    0 讨论(0)
  • 2020-12-08 13:15
     <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.

    0 讨论(0)
  • 2020-12-08 13:20

    When setting a Selected row of a DataGridView at load time, consider handling this in the DataBindingComplete event, because it can be overwritten by default.

    0 讨论(0)
提交回复
热议问题