DataGridView.CurrentCell 属性

定义

获取或设置当前处于活动状态的单元格。

C#
[System.ComponentModel.Browsable(false)]
public System.Windows.Forms.DataGridViewCell CurrentCell { get; set; }
C#
[System.ComponentModel.Browsable(false)]
public System.Windows.Forms.DataGridViewCell? CurrentCell { get; set; }

属性值

表示当前单元格的 DataGridViewCell,如果没有当前单元格,则为 null。 默认值是第一列中的第一个单元格,如果控件中没有单元格,则为 null

属性

例外

不能设置此属性的值,因为不能提交或取消对当前单元格所做的更改。

- 或 -

设置此属性时,指定的单元格在隐藏的行或列中。

仅当 DataGridView 绑定到 DataSource 时,才允许重入调用。 对基础数据进行更改将导致重入调用。

设置此属性时,指定的单元格不在 DataGridView 中。

示例

下面的代码示例演示如何通过将 属性设置为 FirstDisplayedCell 通过 CurrentCell 属性检索到的单元格,确保排序后当前单元格可见。 此代码仅适用于未绑定 DataGridView 控件。 DataGridView如果控件绑定到外部数据源,则排序时不会自动保留当前单元格。

若要运行此示例,请将以下代码粘贴到包含 DataGridView 名为 dataGridView1的窗体中。 在 C# 中,还必须将 Sorted 事件连接到事件处理程序。

C#
private void dataGridView1_Sorted(object sender, EventArgs e)
{
    this.dataGridView1.FirstDisplayedCell = this.dataGridView1.CurrentCell;
}

注解

将单元格设置为当前单元格时,如果当前未显示该单元格,它将滚动到视图中。 当前单元格不能是标题单元格、禁用单元格或隐藏行或列中的单元格。

可以将此属性设置为 null 以暂时删除焦点矩形,但当控件收到焦点并且此属性 null的值为 时,它会自动设置为 属性的值 FirstDisplayedCell

更改此属性的值时,事件发生 SelectionChangedCurrentCellChanged 事件之前。 此时访问CurrentCell属性的任何SelectionChanged事件处理程序都将获取其以前的值。

将 绑定到 DataGridView 外部数据源时,此属性将重置为其默认值。

适用于

产品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另请参阅