繁体   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