DataGridViewDataErrorEventArgs Classe

Definição

Fornece dados para o evento de 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
Herança

Exemplos

O exemplo de código a seguir demonstra como responder às informações fornecidas pela DataGridViewDataErrorEventArgs classe . Este exemplo faz parte de um exemplo maior disponível no tópico de visão geral da 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

Comentários

Lidar com o DataError evento permite manipular exceções geradas por código fora do controle (por exemplo, por uma fonte de dados externa). Use a Context propriedade para determinar o estado do DataGridView no momento da exceção. Use a Exception propriedade para recuperar os dados de exceção. Se você quiser manipular a exceção por manipuladores de eventos adicionais, defina a ThrowException propriedade como true.

As ColumnIndex propriedades e RowIndex normalmente indicam a célula na qual ocorreu o erro de dados. No entanto, quando o erro ocorre em uma fonte de dados externa, a fonte de dados pode não fornecer a coluna na qual o erro ocorreu. Nesse caso, a ColumnIndex propriedade normalmente indica a coluna da célula atual no momento do erro.

Construtores

DataGridViewDataErrorEventArgs(Exception, Int32, Int32, DataGridViewDataErrorContexts)

Inicializa uma nova instância da classe DataGridViewDataErrorEventArgs.

Propriedades

Cancel

Obtém ou define um valor que indica se o evento deve ser cancelado.

(Herdado de CancelEventArgs)
ColumnIndex

Obtém o índice da coluna da célula para a qual o evento ocorre.

(Herdado de DataGridViewCellCancelEventArgs)
Context

Obtém detalhes sobre o estado do DataGridView quando o erro ocorreu.

Exception

Obtém a exceção que representa o erro.

RowIndex

Obtém o índice da linha da célula para a qual o evento ocorre.

(Herdado de DataGridViewCellCancelEventArgs)
ThrowException

Obtém ou define um valor que indica se uma exceção deve ser gerada após o delegado DataGridViewDataErrorEventHandler terminar com ele.

Métodos

Equals(Object)

Determina se o objeto especificado é igual ao objeto atual.

(Herdado de Object)
GetHashCode()

Serve como a função de hash padrão.

(Herdado de Object)
GetType()

Obtém o Type da instância atual.

(Herdado de Object)
MemberwiseClone()

Cria uma cópia superficial do Object atual.

(Herdado de Object)
ToString()

Retorna uma cadeia de caracteres que representa o objeto atual.

(Herdado de Object)

Aplica-se a

Confira também