DataGridView.CurrentCell 属性

定义

获取或设置当前处于活动状态的单元格。Gets or sets the currently active cell.

public:
 property System::Windows::Forms::DataGridViewCell ^ CurrentCell { System::Windows::Forms::DataGridViewCell ^ get(); void set(System::Windows::Forms::DataGridViewCell ^ value); };
[System.ComponentModel.Browsable(false)]
public System.Windows.Forms.DataGridViewCell CurrentCell { get; set; }
member this.CurrentCell : System.Windows.Forms.DataGridViewCell with get, set
Public Property CurrentCell As DataGridViewCell

属性值

表示当前单元格的 DataGridViewCell,如果没有当前单元格,则为 nullThe DataGridViewCell that represents the current cell, or null if there is no current cell. 默认值是第一列中的第一个单元格,如果控件中没有单元格,则为 nullThe default is the first cell in the first column or null if there are no cells in the control.

属性

异常

不能设置此属性的值,因为不能提交或取消对当前单元格所做的更改。The value of this property cannot be set because changes to the current cell cannot be committed or canceled.

- 或 --or- 设置此属性时,指定的单元格在隐藏的行或列中。The specified cell when setting this property is in a hidden row or column.

仅当 DataGridView 绑定到 DataSource 时,才允许重入调用。Re-entrant calling is only allowed when the DataGridView is bound to a DataSource. 对基础数据进行更改将导致重入调用。Re-entrant calling results from a change to the underlying data.

设置此属性时,指定的单元格不在 DataGridView 中。The specified cell when setting this property is not in the DataGridView.

示例

下面的代码示例演示了如何通过将 FirstDisplayedCell 属性设置为通过 @no__t 属性检索到的单元格来确保当前单元格在排序后可见。The following code example illustrates how to ensure that the current cell is visible after sorting by setting the FirstDisplayedCell property to the cell retrieved through the CurrentCell property. 此代码仅适用于未绑定的 DataGridView 控件。This code works only with an unbound DataGridView control. 如果将 @no__t 0 控件绑定到外部数据源,则在排序时不会自动保留当前单元格。With a DataGridView control bound to an external data source, the current cell is not automatically persisted when sorting.

若要运行此示例,请将以下代码粘贴到包含名为 @no__t 的 @no__t 0 的窗体中。To run this example, paste the following code into a form that contains a DataGridView named dataGridView1. 在C#中,还必须将 Sorted 事件连接到事件处理程序。In C#, you must also connect the Sorted event to the event handler.

private void dataGridView1_Sorted(object sender, EventArgs e)
{
    this.dataGridView1.FirstDisplayedCell = this.dataGridView1.CurrentCell;
}
Private Sub dataGridView1_Sorted(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles dataGridView1.Sorted

    Me.dataGridView1.FirstDisplayedCell = Me.dataGridView1.CurrentCell

End Sub

注解

将单元格设置为当前单元格时,它将滚动到视图(如果当前未显示)。When you set a cell as the current cell, it will scroll into view if it is not currently displayed. 当前单元不能是标题单元、禁用的单元格或隐藏的行或列中的单元。The current cell cannot be a header cell, a disabled cell, or a cell in a hidden row or column.

您可以将此属性设置为 null 以暂时删除聚焦框,但当控件接收焦点并且此属性的值 @no__t 为时,它会自动设置为 FirstDisplayedCell 属性的值。You can set this property to null to temporarily remove the focus rectangle, but when the control receives focus and the value of this property is null, it is automatically set to the value of the FirstDisplayedCell property.

更改此属性的值时,@no__t @no__t 事件发生在1事件之前。When you change the value of this property, the SelectionChanged event occurs before the CurrentCellChanged event. 此时访问 @no__t @no__t 属性的任何事件处理程序都将获得其以前的值。Any SelectionChanged event handler accessing the CurrentCell property at this time will get its previous value.

DataGridView 绑定到外部数据源时,此属性将重置为其默认值。When you bind the DataGridView to an external data source, this property is reset to its default value.

适用于

另请参阅