DataGridView.CurrentCell 屬性

定義

取得或設定目前的現用儲存格。

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; }
[System.ComponentModel.Browsable(false)]
public System.Windows.Forms.DataGridViewCell? CurrentCell { get; set; }
[<System.ComponentModel.Browsable(false)>]
member this.CurrentCell : System.Windows.Forms.DataGridViewCell with get, set
Public Property CurrentCell As DataGridViewCell

屬性值

DataGridViewCell,表示目前的儲存格;如果沒有目前的儲存格,則為 null。 預設為第一個資料行中的第一個儲存格,如果控制項中沒有任何儲存格,則為 null

屬性

例外狀況

因為無法認可或取消目前儲存格的變更,所以無法設定這個屬性的值。

-或-

設定這個屬性時所指定的儲存格位於隱藏的資料列或資料行中。

僅當 DataGridView 已繫結至 DataSource 時,才允許可重新進入的呼叫。 變更基礎資料產生可重新進入的呼叫。

設定這個屬性時,指定的儲存格不在 DataGridView 中。

範例

下列程式碼範例說明如何將 屬性設定 FirstDisplayedCell 為透過 CurrentCell 屬性擷取的儲存格,確保目前儲存格在排序之後可見。 此程式碼只適用于未系結 DataGridView 的控制項。 DataGridView當控制項系結至外部資料源時,目前儲存格不會在排序時自動儲存。

若要執行此範例,請將下列程式碼貼到包含具名 dataGridView1DataGridView 表單中。 在 C# 中,您也必須將 Sorted 事件連接到事件處理常式。

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

備註

當您將儲存格設定為目前儲存格時,如果目前未顯示,則會捲動到檢視中。 目前的儲存格不能是標題儲存格、停用的儲存格,或是隱藏列或欄中的儲存格。

您可以將這個屬性設定為 null 以暫時移除焦點矩形,但是當控制項收到焦點且此屬性的值是 null 時,它會自動設定為 屬性的值 FirstDisplayedCell

當您變更此屬性的值時,事件 SelectionChanged 會在事件之前 CurrentCellChanged 發生。 此時存取 CurrentCell 屬性的任何 SelectionChanged 事件處理常式都會取得其先前的值。

當您將 系結 DataGridView 至外部資料源時,這個屬性會重設為其預設值。

適用於

另請參閱