DataGridView.CurrentCell 屬性


取得或設定目前的現用儲存格。Gets or sets the currently active cell.

 property System::Windows::Forms::DataGridViewCell ^ CurrentCell { System::Windows::Forms::DataGridViewCell ^ get(); void set(System::Windows::Forms::DataGridViewCell ^ value); };
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 屬性設定為透過屬性抓取的儲存格,以確保目前的儲存格可顯示 CurrentCellThe 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. DataGridView當控制項系結至外部資料源時,當排序時,不會自動儲存目前的儲存格。With a DataGridView control bound to an external data source, the current cell is not automatically persisted when sorting.

若要執行這個範例,請將下列程式碼貼入包含名為的表單中 DataGridView dataGridView1To 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 以暫時移除焦點矩形,但是當控制項收到焦點且這個屬性的值為時 null ,就會自動設定為屬性的值 FirstDisplayedCellYou 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.

當您變更這個屬性的值時, SelectionChanged 事件會發生在 CurrentCellChanged 事件之前。When you change the value of this property, the SelectionChanged event occurs before the CurrentCellChanged event. 此時 SelectionChanged 存取屬性的任何事件處理常式 CurrentCell 都會取得其先前的值。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.