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变量将保留对对应于该行的客户对象的引用。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.

适用于

另请参阅