DataGridView.CancelRowEdit 事件

定义

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.

适用于

另请参阅