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. 如果用户正在编辑的行是用于新记录的行,但是,丢弃旧的客户对象的值,并将替换为一个新,以便用户可以开始再次进行编辑。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.

适用于

另请参阅