DataGridView.CommitEdit(DataGridViewDataErrorContexts) Metoda

Definicja

Zatwierdza zmiany w bieżącej komórce w pamięci podręcznej danych bez zakończenia trybu edycji.

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

Parametry

context
DataGridViewDataErrorContexts

Bitowa kombinacja DataGridViewDataErrorContexts wartości, która określa kontekst, w którym może wystąpić błąd.

Zwraca

true jeżeli zmiany zostały zatwierdzone; w przeciwnym razie false.

Wyjątki

Nie można zadeklarować wartości komórki i nie ma procedury obsługi dla DataError zdarzenia lub program obsługi ustawił ThrowException właściwość na true.

Przykłady

Poniższy przykład kodu wywołuje metodę CommitEdit w procedurze CurrentCellDirtyStateChanged obsługi zdarzeń, aby zgłosić CellValueChanged zdarzenie. Ten przykład kodu jest częścią większego przykładu przedstawionego w temacie How to: Disable Buttons in a Button Column in the Windows Forms DataGridView Control (Jak wyłączyć przyciski w kolumnie przycisku w kontrolce 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

Uwagi

Ta metoda próbuje przekonwertować sformatowaną, określoną przez użytkownika wartość na podstawowy typ danych komórki. W tym celu zgłasza CellParsing zdarzenie, które można obsłużyć w celu dostosowania konwersji typów. W przeciwnym razie są używane konwertery typów domyślnych. Błędy konwersji mogą spowodować wyjątek, jeśli DataError zdarzenie nie jest obsługiwane, aby go zapobiec. Jeśli wartość zostanie pomyślnie przekonwertowana, zostanie zatwierdzona w magazynie danych, co zwiększa CellValuePushed zdarzenie dla komórek niezwiązanych z danymi, gdy VirtualMode wartość właściwości to true. Jeśli wartość zostanie pomyślnie zatwierdzona CellValueChanged , wystąpi zdarzenie.

Dotyczy

Zobacz też