选择它时如何以编程方式将焦点设置在 WinForms DataGrid 中的一行上?
How to programmatically set focus on a row in a WinForms DataGrid when selecting it?
我有一个 WinForms DataGrid(不是 DataGridView)。当我手动 select 行时,行指示器(如下面第一张图片所示)显示在 select 编辑的行上。但是,如果我在保存后设置 DataGrid 的数据源并以编程方式 select 第二行使用:
datagrid.Select(1)
,第二行获得突出显示的背景颜色,但焦点指示器位于第一行,如下面第二张图片所示。
有没有办法使 selected 行获得焦点并显示该行的指示器?
由于这是旧的 System.Windows.Forms.DataGrid,Row selection 方法与 DataGridView 的方法略有不同。
您可以使用 Select() 方法 select 一行。
这不会更改当前行。要使行成为当前行,您可以使用 CurrentRowIndex 属性.
结合起来,这两个移动 selection 并设置当前行。
// 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;
我有一个 WinForms DataGrid(不是 DataGridView)。当我手动 select 行时,行指示器(如下面第一张图片所示)显示在 select 编辑的行上。但是,如果我在保存后设置 DataGrid 的数据源并以编程方式 select 第二行使用:
datagrid.Select(1)
,第二行获得突出显示的背景颜色,但焦点指示器位于第一行,如下面第二张图片所示。
有没有办法使 selected 行获得焦点并显示该行的指示器?
由于这是旧的 System.Windows.Forms.DataGrid,Row selection 方法与 DataGridView 的方法略有不同。
您可以使用 Select() 方法 select 一行。
这不会更改当前行。要使行成为当前行,您可以使用 CurrentRowIndex 属性.
结合起来,这两个移动 selection 并设置当前行。
// 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;