DataGridView.DataError Event

Definition

在以下情况下发生:当外部数据分析或验证操作引发异常时,或者当尝试将数据提交到数据源失败时。Occurs when an external data-parsing or validation operation throws an exception, or when an attempt to commit data to a data source fails.

public:
 event System::Windows::Forms::DataGridViewDataErrorEventHandler ^ DataError;
public event System.Windows.Forms.DataGridViewDataErrorEventHandler DataError;
member this.DataError : System.Windows.Forms.DataGridViewDataErrorEventHandler 
Public Custom Event DataError As DataGridViewDataErrorEventHandler 

Examples

下面的代码示例演示 DataError 事件处理程序。The following code example demonstrates a DataError event handler. 此示例是 DataGridViewComboBoxColumn 类概述主题中提供的一个更大示例的一部分。This example is part of a larger example available in the DataGridViewComboBoxColumn class overview topic.

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

Remarks

使用 DataError 事件可以处理在数据处理操作期间由控件调用的代码引发的异常。The DataError event enables you to handle exceptions thrown in code that is called by the control during data processing operations.

有关如何处理事件的详细信息,请参阅处理和引发事件For more information about how to handle events, see Handling and Raising Events.

Note

与此事件关联的 DataGridViewDataErrorEventArgs 对象的 ColumnIndexRowIndex 属性通常指示发生了数据错误的单元格。The ColumnIndex and RowIndex properties of the DataGridViewDataErrorEventArgs object associated with this event normally indicate the cell in which the data error occurred. 但是,在外部数据源中出现错误时,数据源可能不提供发生错误的列。When the error occurs in an external data source, however, the data source may not provide the column in which the error occurred. 在这种情况下,ColumnIndex 属性通常指示在发生错误时当前单元的列。In this case, the ColumnIndex property typically indicates the column of the current cell at the time of the error.

Applies to

See also