簡體   English   中英

以編程方式在 DataGridView 中選擇一行

[英]Selecting a row in DataGridView programmatically

如何在運行時以編程方式 select DataGridView中的特定行范圍?

未經測試,但我認為您可以執行以下操作:

dataGrid.Rows[index].Selected = true;

或者您可以執行以下操作(但再次:未測試):

dataGrid.SelectedRows.Clear();
foreach(DataGridViewRow row in dataGrid.Rows)
{
    if(YOUR CONDITION)
       row.Selected = true;
}

在 Visual Basic 中,對 select 在DataGridView中的一行執行此操作; 所選行將以突出顯示的顏色顯示,但請注意 cursor position 不會改變:

Grid.Rows(0).Selected = True

執行此更改 cursor 的 position:

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

結合上面的行將 position cursor 和 select 一行。 這是在DataGridView中聚焦和選擇一行的標准過程:

Grid.CurrentCell = Grid.Rows(0).Cells(0)
Grid.Rows(0).Selected = True
DataGridView.Rows
    .OfType<DataGridViewRow>()
     .Where(x => (int)x.Cells["Id"].Value == pId)
     .ToArray<DataGridViewRow>()[0]
     .Selected = true;
 <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. 清除所有先前的選擇。
  2. 循環遍歷需要在網格中選擇的行。
  3. 從網格獲取他們的行句柄(注意這里的網格已經用新行更新)
  4. 檢查行句柄是否有效。
  5. 當有效的行句柄然后 select 它。

其中 itemList 是要在網格視圖中選擇的行列表。

嘗試這個:

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

如果您有數據源,則可以使用 Select 方法: http://msdn.microsoft.com/en-us/library/b51xae2y%28v=vs.71%29.aspx

如果數據源中有對象,或者使用 linq

嘗試這個:

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

希望這有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM