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 值的位元 (Bitwise) 組合,指定可能發生錯誤的內容。

傳回

如果已認可變更,則為 true;否則為 false

例外狀況

無法認可儲存格值,DataError 事件沒有處理常式,或處理常式已將 ThrowException 屬性設定為 true

範例

下列程式碼範例會在 CommitEdit 事件處理常式內 CurrentCellDirtyStateChanged 呼叫 方法,以引發 CellValueChanged 事件。 此程式碼範例是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

備註

這個方法會嘗試將格式化的使用者指定值轉換為基礎資料格資料類型。 若要這樣做,它會引發 CellParsing 事件,您可以處理此事件來自訂類型轉換。 否則,會使用預設類型轉換器。 如果 DataError 事件未處理以避免發生,轉換錯誤可能會導致例外狀況。 如果成功轉換值,則會將其認可至資料存放區,並在屬性值為 trueVirtualMode 引發 CellValuePushed 非資料繫結儲存格的事件。 如果成功認可值,就會 CellValueChanged 發生事件。

適用於

另請參閱