DataGridView.CancelRowEdit DataGridView.CancelRowEdit DataGridView.CancelRowEdit DataGridView.CancelRowEdit Event

定義

DataGridView コントロールの VirtualMode プロパティが true で、行の編集をキャンセルした場合に発生します。Occurs when the VirtualMode property of a DataGridView control is true and the cancels edits in a row.

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コントロールでこのイベントを処理する方法を示しています。The following code example illustrates how to handle this event for a DataGridView control in virtual mode. コントロールが編集モードの場合、変数はrowInEdit編集されている行のインデックスを保持し、 customerInEdit変数はその行に対応する Customer オブジェクトへの参照を保持します。When the control is in edit mode, the rowInEdit variable holds the index of the row being edited, and the customerInEdit variable holds a reference to a Customer object corresponding to that row. ユーザーが編集モードをキャンセルした場合、このオブジェクトは破棄されます。When the user cancels out of edit mode, this object can be discarded. ただし、ユーザーが編集していた行が新しいレコードの行である場合、古い Customer オブジェクトは破棄されて新しいレコードに置き換えられるため、ユーザーはもう一度編集を開始できます。If the row the user was editing is the row for new records, however, the old Customer object is discarded and replaced with a new one so that the user can begin making edits again. この例は、チュートリアルで使用できるより大きな例の一部です。Windows フォーム DataGridView コントロールでの仮想モードの実装。This example is part of a larger example available in Walkthrough: Implementing Virtual Mode in the Windows Forms DataGridView Control.

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が仮想モードの場合、既定では、変更はセルレベルでデータキャッシュにコミットされます。When the DataGridView is in virtual mode, changes are committed to the data cache at the cell level by default. このCancelRowEditイベントは、行レベルのトランザクションを実装するときに使用できます。The CancelRowEdit event can be used when implementing row-level transactions.

イベントを処理する方法の詳細については、次を参照してください。処理とイベントの発生します。For more information about how to handle events, see Handling and Raising Events.

適用対象

こちらもご覧ください