DataGridViewDataErrorEventArgs Classe

Definizione

Fornisce i dati per l'evento DataError.

public ref class DataGridViewDataErrorEventArgs : System::Windows::Forms::DataGridViewCellCancelEventArgs
public class DataGridViewDataErrorEventArgs : System.Windows.Forms.DataGridViewCellCancelEventArgs
type DataGridViewDataErrorEventArgs = class
    inherit DataGridViewCellCancelEventArgs
Public Class DataGridViewDataErrorEventArgs
Inherits DataGridViewCellCancelEventArgs
Ereditarietà

Esempio

Nell'esempio DataGridViewDataErrorEventArgs di codice seguente viene illustrato come rispondere alle informazioni fornite dalla classe. Questo esempio fa parte di un esempio più ampio disponibile nell'argomento panoramica della DataGridViewComboBoxColumn classe.

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

Commenti

La gestione dell'evento DataError consente di gestire le eccezioni generate dal codice esterno al controllo, ad esempio da un'origine dati esterna. Utilizzare la Context proprietà per determinare lo stato dell'oggetto DataGridView al momento dell'eccezione. Utilizzare la proprietà per recuperare i dati dell'eccezione Exception . Se si vuole gestire l'eccezione da gestori eventi aggiuntivi, impostare la ThrowException proprietà su true.

Le ColumnIndex proprietà e RowIndex in genere indicano la cella in cui si è verificato l'errore dei dati. Quando si verifica l'errore in un'origine dati esterna, tuttavia, l'origine dati potrebbe non fornire la colonna in cui si è verificato l'errore. In questo caso, la ColumnIndex proprietà indica in genere la colonna della cella corrente al momento dell'errore.

Costruttori

DataGridViewDataErrorEventArgs(Exception, Int32, Int32, DataGridViewDataErrorContexts)

Inizializza una nuova istanza della classe DataGridViewDataErrorEventArgs.

Proprietà

Cancel

Ottiene o imposta un valore che indica se l'evento debba essere annullato.

(Ereditato da CancelEventArgs)
ColumnIndex

Ottiene l'indice della colonna della cella per la quale si verifica l'evento.

(Ereditato da DataGridViewCellCancelEventArgs)
Context

Ottiene i dettagli sullo stato dell'oggetto DataGridView quando si è verificato l'errore.

Exception

Ottiene l'eccezione che rappresenta l'errore.

RowIndex

Ottiene l'indice della riga della cella per la quale si verifica l'evento.

(Ereditato da DataGridViewCellCancelEventArgs)
ThrowException

Ottiene o imposta un valore che indica se generare l'eccezione dopo che il delegato DataGridViewDataErrorEventHandler ne ha ultimata l'elaborazione.

Metodi

Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.

(Ereditato da Object)
GetHashCode()

Funge da funzione hash predefinita.

(Ereditato da Object)
GetType()

Ottiene l'oggetto Type dell'istanza corrente.

(Ereditato da Object)
MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.

(Ereditato da Object)
ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.

(Ereditato da Object)

Si applica a

Vedi anche