DataGridView.CancelRowEdit Event

Definition

Occurs when the VirtualMode property of a DataGridView control is true and the user cancels edits in a row.

public event System.Windows.Forms.QuestionEventHandler CancelRowEdit;
public event System.Windows.Forms.QuestionEventHandler? CancelRowEdit;

Event Type

Examples

The following code example illustrates how to handle this event for a DataGridView control in virtual mode. 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. This example is part of a larger example available in Walkthrough: Implementing Virtual Mode in the Windows Forms DataGridView Control.

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;
    }
}

Remarks

When the DataGridView is in virtual mode, changes are committed to the data cache at the cell level by default. The CancelRowEdit event can be used when implementing row-level transactions.

For more information about how to handle events, see Handling and Raising Events.

Applies to

Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

See also