简体   繁体   English

选择它时如何以编程方式将焦点设置在 WinForms DataGrid 中的一行上?

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

I have a WinForms DataGrid (not DataGridView).我有一个 WinForms DataGrid(不是 DataGridView)。 When I manually select a row, the row indicator, shown in the first image below, shows on the selected rows.当我手动 select 一行时,行指示器(如下面第一张图片所示)显示在所选行上。 However, if I set the DataGrid's datasource after a save and programmatically select the second row using: datagrid.Select(1) , the second row gets the highlighted background color but the focus indicator is on the first row as shown in the second image below.但是,如果我在保存后设置 DataGrid 的数据源并以编程方式 select 第二行使用: datagrid.Select(1) ,第二行获得突出显示的背景颜色但焦点指示器位于第一行,如下面的第二张图片所示.

Is there a way to make the selected row get focus and have the indicator display for the row?有没有办法使所选行获得焦点并显示该行的指示器?

在此处输入图像描述

在此处输入图像描述

Since this is the old System.Windows.Forms.DataGrid , the Row selection method is slightly different than the DataGridView's.由于这是旧的System.Windows.Forms.DataGrid ,因此行选择方法与 DataGridView 略有不同。

You can select a Row, as you're doing, with the Select() method.您可以使用Select()方法 select 一行。
This doesn't change the Current Row.这不会更改当前行。 To make a Row the Current, you can use the CurrentRowIndex property.要使行成为当前行,您可以使用CurrentRowIndex属性。
Combined, these two move the selection and set the Current Row.结合起来,这两个移动选择并设置当前行。

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

Something similar in a DataGridView: DataGridView 中的类似内容:
(One of the methods that can be used to achieve this result) (可用于实现此结果的方法之一)

// 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];

Try this:尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM