DataGridView.CommitEdit(DataGridViewDataErrorContexts) Method

Definition

Commits changes in the current cell to the data cache without ending edit mode.

C#
public bool CommitEdit(System.Windows.Forms.DataGridViewDataErrorContexts context);

Parameters

context
DataGridViewDataErrorContexts

A bitwise combination of DataGridViewDataErrorContexts values that specifies the context in which an error can occur.

Returns

true if the changes were committed; otherwise false.

Exceptions

The cell value could not be committed and either there is no handler for the DataError event or the handler has set the ThrowException property to true.

Examples

The following code example calls the CommitEdit method within a CurrentCellDirtyStateChanged event handler to raise the CellValueChanged event. This code example is part of a larger example provided in How to: Disable Buttons in a Button Column in the Windows Forms DataGridView Control.

C#
// This event handler manually raises the CellValueChanged event
// by calling the CommitEdit method.
void dataGridView1_CurrentCellDirtyStateChanged(object sender,
    EventArgs e)
{
    if (dataGridView1.IsCurrentCellDirty)
    {
        dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
    }
}

// If a check box cell is clicked, this event handler disables  
// or enables the button in the same row as the clicked cell.
public void dataGridView1_CellValueChanged(object sender,
    DataGridViewCellEventArgs e)
{
    if (dataGridView1.Columns[e.ColumnIndex].Name == "CheckBoxes")
    {
        DataGridViewDisableButtonCell buttonCell =
            (DataGridViewDisableButtonCell)dataGridView1.
            Rows[e.RowIndex].Cells["Buttons"];

        DataGridViewCheckBoxCell checkCell =
            (DataGridViewCheckBoxCell)dataGridView1.
            Rows[e.RowIndex].Cells["CheckBoxes"];
        buttonCell.Enabled = !(Boolean)checkCell.Value;

        dataGridView1.Invalidate();
    }
}

Remarks

This method attempts to convert the formatted, user-specified value to the underlying cell data type. To do this, it raises the CellParsing event, which you can handle to customize the type conversion. Otherwise, default type converters are used. Conversion errors may result in an exception if the DataError event is not handled to prevent it. If the value is successfully converted, it is committed to the data store, raising the CellValuePushed event for non-data-bound cells when the VirtualMode property value is true. If the value is successfully committed, the CellValueChanged event occurs.

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, 10

See also