DataGridViewDataErrorEventArgs.Context Eigenschaft

Definition

Ruft Details über den Zustand ab, in dem sich die DataGridView beim Auftreten des Fehlers befand.

public:
 property System::Windows::Forms::DataGridViewDataErrorContexts Context { System::Windows::Forms::DataGridViewDataErrorContexts get(); };
public System.Windows.Forms.DataGridViewDataErrorContexts Context { get; }
member this.Context : System.Windows.Forms.DataGridViewDataErrorContexts
Public ReadOnly Property Context As DataGridViewDataErrorContexts

Eigenschaftswert

Eine bitweise Kombination von DataGridViewDataErrorContexts-Werten, die den Kontext angibt, in dem der Fehler aufgetreten ist.

Beispiele

Im folgenden Codebeispiel wird veranschaulicht, wie der Fehlerkontext untersucht wird. Dieses Beispiel ist Teil eines größeren Beispiels, das im Thema Klassenübersicht DataGridViewComboBoxColumn verfügbar ist.

private:
    void DataGridView1_DataError(Object^ sender, DataGridViewDataErrorEventArgs^ anError)
    {

        MessageBox::Show("Error happened " + anError->Context.ToString());

        if (anError->Context == DataGridViewDataErrorContexts::Commit)
        {
            MessageBox::Show("Commit error");
        }
        if (anError->Context == DataGridViewDataErrorContexts::CurrentCellChange)
        {
            MessageBox::Show("Cell change");
        }
        if (anError->Context == DataGridViewDataErrorContexts::Parsing)
        {
            MessageBox::Show("parsing error");
        }
        if (anError->Context == DataGridViewDataErrorContexts::LeaveControl)
        {
            MessageBox::Show("leave control error");
        }

        if (dynamic_cast<ConstraintException^>(anError->Exception) != nullptr)
        {
            DataGridView^ view = (DataGridView^)sender;
            view->Rows[anError->RowIndex]->ErrorText = "an error";
            view->Rows[anError->RowIndex]->Cells[anError->ColumnIndex]->ErrorText = "an error";

            anError->ThrowException = false;
        }
    }
private void DataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs anError)
{

    MessageBox.Show("Error happened " + anError.Context.ToString());

    if (anError.Context == DataGridViewDataErrorContexts.Commit)
    {
        MessageBox.Show("Commit error");
    }
    if (anError.Context == DataGridViewDataErrorContexts.CurrentCellChange)
    {
        MessageBox.Show("Cell change");
    }
    if (anError.Context == DataGridViewDataErrorContexts.Parsing)
    {
        MessageBox.Show("parsing error");
    }
    if (anError.Context == DataGridViewDataErrorContexts.LeaveControl)
    {
        MessageBox.Show("leave control error");
    }

    if ((anError.Exception) is ConstraintException)
    {
        DataGridView view = (DataGridView)sender;
        view.Rows[anError.RowIndex].ErrorText = "an error";
        view.Rows[anError.RowIndex].Cells[anError.ColumnIndex].ErrorText = "an error";

        anError.ThrowException = false;
    }
}
Private Sub DataGridView1_DataError(ByVal sender As Object, _
ByVal e As DataGridViewDataErrorEventArgs) _
Handles DataGridView1.DataError

    MessageBox.Show("Error happened " _
        & e.Context.ToString())

    If (e.Context = DataGridViewDataErrorContexts.Commit) _
        Then
        MessageBox.Show("Commit error")
    End If
    If (e.Context = DataGridViewDataErrorContexts _
        .CurrentCellChange) Then
        MessageBox.Show("Cell change")
    End If
    If (e.Context = DataGridViewDataErrorContexts.Parsing) _
        Then
        MessageBox.Show("parsing error")
    End If
    If (e.Context = _
        DataGridViewDataErrorContexts.LeaveControl) Then
        MessageBox.Show("leave control error")
    End If

    If (TypeOf (e.Exception) Is ConstraintException) Then
        Dim view As DataGridView = CType(sender, DataGridView)
        view.Rows(e.RowIndex).ErrorText = "an error"
        view.Rows(e.RowIndex).Cells(e.ColumnIndex) _
            .ErrorText = "an error"

        e.ThrowException = False
    End If
End Sub

Hinweise

Member der DataGridViewDataErrorContexts -Enumeration können mithilfe des bitweisen OR Operators kombiniert werden, um den Zustand einer datengebundenen DataGridView Instanz darzustellen, wenn ein Datenfehler aufgetreten ist. Wenn ein Benutzer beispielsweise einen ungültigen Zellwert eingibt (z. B. einen Namen in einer Zelle eingibt, für die ein Datum erforderlich ist) und dann eine andere Zelle auswählt, wird versucht, DataGridView den ungültigen Zellwert zu committen. Wenn der Commit fehlschlägt, löst ein DataGridView -Ereignis aus DataError , dessen Context Eigenschaft den Wert Commit und CurrentCellChangeaufweist.

Gilt für:

Weitere Informationen