DataGridView.CancelRowEdit 事件

定義

發生於 DataGridView 控制項的 VirtualMode 屬性為 true,且取消資料列中的編輯時。

public:
 event System::Windows::Forms::QuestionEventHandler ^ CancelRowEdit;
public event System.Windows.Forms.QuestionEventHandler CancelRowEdit;
public event System.Windows.Forms.QuestionEventHandler? CancelRowEdit;
member this.CancelRowEdit : System.Windows.Forms.QuestionEventHandler 
Public Custom Event CancelRowEdit As QuestionEventHandler 

事件類型

範例

下列程式碼範例說明如何在虛擬模式中處理控制項的這個事件 DataGridView 。 當控制項處於編輯模式時, rowInEdit 變數會保留所編輯之資料列的索引,而 customerInEdit 變數會保存對應至該資料列之 Customer 物件的參考。 當使用者取消編輯模式時,可以捨棄此物件。 不過,如果使用者編輯的資料列是新記錄的資料列,則會捨棄舊的 Customer 物件,並以新的物件取代,讓使用者可以再次開始編輯。 此範例是逐步解說:在 Windows Forms DataGridView 控制項中實作虛擬模式的較大型範例的一部分。

void dataGridView1_CancelRowEdit( Object^ /*sender*/,
    System::Windows::Forms::QuestionEventArgs^ /*e*/ )
{
   if ( this->rowInEdit == this->dataGridView1->Rows->Count - 2 &&
        this->rowInEdit == this->customers->Count )
   {
      
      // If the user has canceled the edit of a newly created row, 
      // replace the corresponding Customer object with a new, empty one.
      this->customerInEdit = gcnew Customer;
   }
   else
   {
      
      // If the user has canceled the edit of an existing row, 
      // release the corresponding Customer object.
      this->customerInEdit = nullptr;
      this->rowInEdit = -1;
   }
}
private void dataGridView1_CancelRowEdit(object sender,
    System.Windows.Forms.QuestionEventArgs e)
{
    if (this.rowInEdit == this.dataGridView1.Rows.Count - 2 &&
        this.rowInEdit == this.customers.Count)
    {
        // If the user has canceled the edit of a newly created row, 
        // replace the corresponding Customer object with a new, empty one.
        this.customerInEdit = new Customer();
    }
    else
    {
        // If the user has canceled the edit of an existing row, 
        // release the corresponding Customer object.
        this.customerInEdit = null;
        this.rowInEdit = -1;
    }
}
Private Sub dataGridView1_CancelRowEdit(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.QuestionEventArgs) _
    Handles dataGridView1.CancelRowEdit

    If Me.rowInEdit = Me.dataGridView1.Rows.Count - 2 AndAlso _
        Me.rowInEdit = Me.customers.Count Then

        ' If the user has canceled the edit of a newly created row, 
        ' replace the corresponding Customer object with a new, empty one.
        Me.customerInEdit = New Customer()

    Else

        ' If the user has canceled the edit of an existing row, 
        ' release the corresponding Customer object.
        Me.customerInEdit = Nothing
        Me.rowInEdit = -1

    End If

End Sub

備註

DataGridView當 處於虛擬模式時,預設會將變更認可至資料格層級的資料快取。 實作資料列層級交易時,可以使用 事件 CancelRowEdit

如需如何處理事件的詳細資訊,請參閱 處理和引發事件

適用於

另請參閱