DataGridView.CommitEdit(DataGridViewDataErrorContexts) 方法

定义

将当前单元格中的更改提交到数据缓存,但不结束编辑模式。Commits changes in the current cell to the data cache without ending edit mode.

public:
 bool CommitEdit(System::Windows::Forms::DataGridViewDataErrorContexts context);
public bool CommitEdit (System.Windows.Forms.DataGridViewDataErrorContexts context);
member this.CommitEdit : System.Windows.Forms.DataGridViewDataErrorContexts -> bool
Public Function CommitEdit (context As DataGridViewDataErrorContexts) As Boolean

参数

context
DataGridViewDataErrorContexts

DataGridViewDataErrorContexts 值的按位组合,指定可能出错的上下文。A bitwise combination of DataGridViewDataErrorContexts values that specifies the context in which an error can occur.

返回

如果更改已提交,为 true;否则为 falsetrue if the changes were committed; otherwise false.

例外

无法提交单元格值,且没有 DataError 事件的处理程序或处理程序已将 ThrowException 属性设置为 trueThe 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.

示例

下面的代码示例调用 CurrentCellDirtyStateChanged 事件处理程序中的 CommitEdit 方法,以引发 CellValueChanged 事件。The following code example calls the CommitEdit method within a CurrentCellDirtyStateChanged event handler to raise the CellValueChanged event. 此代码示例摘自如何:禁用 Windows 窗体 DataGridView 控件的按钮列中的按钮中提供的一个更大的示例。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.
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();
    }
}
' 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
        dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
    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"), _
            DataGridViewDisableButtonCell)

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

        dataGridView1.Invalidate()
    End If
End Sub

注解

此方法尝试将格式的、用户指定的值转换为基础单元数据类型。This method attempts to convert the formatted, user-specified value to the underlying cell data type. 为此,它会引发 CellParsing 事件,你可以处理该事件以自定义类型转换。To do this, it raises the CellParsing event, which you can handle to customize the type conversion. 否则,将使用默认类型转换器。Otherwise, default type converters are used. 如果未处理 DataError 事件来阻止此事件,则转换错误可能会导致异常。Conversion errors may result in an exception if the DataError event is not handled to prevent it. 如果值已成功转换,则将其提交到数据存储,并在 VirtualMode 属性值 true时为非数据绑定单元引发 CellValuePushed 事件。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. 如果值已成功提交,则会发生 CellValueChanged 事件。If the value is successfully committed, the CellValueChanged event occurs.

适用于

另请参阅