共用方式為


DataGridView.CommitEdit(DataGridViewDataErrorContexts) 方法

定義

在不結束編輯模式的情況下,將目前儲存格的變更提交到資料快取。

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 的值,用來指定錯誤可能發生的上下文。

傳回

true 如果這些變更是被承諾的;否則 false

例外狀況

該儲存格值無法被提交,且事件中沒有處理 DataError 程序,或處理程序已將屬性設定 ThrowExceptiontrue

範例

以下程式碼範例 CommitEdit 呼叫事件處理程序中的 CurrentCellDirtyStateChanged 方法來引發事件 CellValueChanged 。 此程式碼範例是 Windows 表單 DataGridView 控制項中「如何:在按鈕欄位中停用按鈕」中提供較大範例的一部分。

// 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

備註

此方法嘗試將格式化、使用者指定的值轉換為底層儲存格資料型態。 為此,它會提升事件, CellParsing 你可以處理事件以自訂類型轉換。 否則,則使用預設類型的轉換器。 若 DataError 事件未被處理以防止,轉換錯誤可能導致例外。 若值成功轉換,則會提交至資料儲存庫,當VirtualMode屬性值為 true時,非資料綁定儲存格會觸發CellValuePushed事件。 若該值成功提交,事件 CellValueChanged 即會發生。

適用於

另請參閱