簡體   English   中英

選擇它時如何以編程方式將焦點設置在 WinForms DataGrid 中的一行上?

[英]How to programmatically set focus on a row in a WinForms DataGrid when selecting it?

我有一個 WinForms DataGrid(不是 DataGridView)。 當我手動 select 一行時,行指示器(如下面第一張圖片所示)顯示在所選行上。 但是,如果我在保存后設置 DataGrid 的數據源並以編程方式 select 第二行使用: datagrid.Select(1) ,第二行獲得突出顯示的背景顏色但焦點指示器位於第一行,如下面的第二張圖片所示.

有沒有辦法使所選行獲得焦點並顯示該行的指示器?

在此處輸入圖像描述

在此處輸入圖像描述

由於這是舊的System.Windows.Forms.DataGrid ,因此行選擇方法與 DataGridView 略有不同。

您可以使用Select()方法 select 一行。
這不會更改當前行。 要使行成為當前行,您可以使用CurrentRowIndex屬性。
結合起來,這兩個移動選擇並設置當前行。

 // Selects and highlights the Row at index 1
 dataGrid.Select(1);
 // Make the Row at index 1 the Current
 dataGrid.CurrentRowIndex = 1;

DataGridView 中的類似內容:
(可用於實現此結果的方法之一)

// Move the focus and selects the Row at index 1
dataGridView.Rows[1].Selected = true;
// Make the Row at index 1 the Current setting the CurrentCell property
dataGridView.CurrentCell = dgvTest.Rows[1].Cells[0];

嘗試這個:

dataGridView1.FirstDisplayedScrollingRowIndex = 
                                dataGridView1.Rows[dataGridView1.Rows.Count - 2].Index;

暫無
暫無

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

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