DataGridView.CommitEdit Method (DataGridViewDataErrorContexts)


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

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

Public Function CommitEdit (
	context As DataGridViewDataErrorContexts
) As Boolean


Type: System.Windows.Forms.DataGridViewDataErrorContexts

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

Return Value

Type: System.Boolean

true if the changes were committed; otherwise false.

Exception Condition

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

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.

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.

' This event handler manually raises the CellValueChanged event
' by calling the CommitEdit method.
Sub dataGridView1_CurrentCellDirtyStateChanged( _
    ByVal sender As Object, ByVal e As EventArgs) _
    Handles dataGridView1.CurrentCellDirtyStateChanged

    If dataGridView1.IsCurrentCellDirty Then
    End If
End Sub

' If a check box cell is clicked, this event handler disables  
' or enables the button in the same row as the clicked cell.
Public Sub dataGridView1_CellValueChanged(ByVal sender As Object, _
    ByVal e As DataGridViewCellEventArgs) _
    Handles dataGridView1.CellValueChanged

    If dataGridView1.Columns(e.ColumnIndex).Name = "CheckBoxes" Then
        Dim buttonCell As DataGridViewDisableButtonCell = _
            CType(dataGridView1.Rows(e.RowIndex).Cells("Buttons"), _

        Dim checkCell As DataGridViewCheckBoxCell = _
            CType(dataGridView1.Rows(e.RowIndex).Cells("CheckBoxes"), _
        buttonCell.Enabled = Not CType(checkCell.Value, [Boolean])

    End If
End Sub

.NET Framework
Available since 2.0
Return to top