DataGridView.CellEndEdit Event

Definition

Occurs when edit mode stops for the currently selected cell.

C#
public event System.Windows.Forms.DataGridViewCellEventHandler CellEndEdit;
C#
public event System.Windows.Forms.DataGridViewCellEventHandler? CellEndEdit;

Event Type

Examples

The following code example illustrates how to handle this event to clear the row DataGridViewRow.ErrorText property in case it was previously set by a CellValidating event handler. The CellValidating event handler can clear the error text when the new cell value meets the validation criteria, but when the user reverts to the old cell value by pressing ESC, the CellValidating event does not occur. This example is part of a larger example available in Walkthrough: Validating Data in the Windows Forms DataGridView Control.

C#
private void dataGridView1_CellValidating(object sender,
    DataGridViewCellValidatingEventArgs e)
{
    string headerText = 
        dataGridView1.Columns[e.ColumnIndex].HeaderText;

    // Abort validation if cell is not in the CompanyName column.
    if (!headerText.Equals("CompanyName")) return;

    // Confirm that the cell is not empty.
    if (string.IsNullOrEmpty(e.FormattedValue.ToString()))
    {
        dataGridView1.Rows[e.RowIndex].ErrorText =
            "Company Name must not be empty";
        e.Cancel = true;
    }
}

void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    // Clear the row error in case the user presses ESC.   
    dataGridView1.Rows[e.RowIndex].ErrorText = String.Empty;
}

Remarks

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