DataGridViewDataErrorEventArgs 类

定义

DataError 事件提供数据。Provides data for the DataError event.

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
继承

示例

下面的代码示例演示如何响应DataGridViewDataErrorEventArgs类提供的信息。The following code example demonstrates how to respond to information provided by the DataGridViewDataErrorEventArgs class. 此示例是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

注解

通过处理DataError事件, 您可以处理由控件外部的代码引发的异常 (例如, 由外部数据源)。Handling the DataError event lets you handle exceptions thrown by code outside your control (for example, by an external data source). 使用属性可确定发生异常时的DataGridView状态。 ContextUse the Context property to determine the state of the DataGridView at the time of the exception. Exception使用属性可检索异常数据。Use the Exception property to retrieve the exception data. 如果希望通过其他事件处理程序来处理异常, 请将ThrowException属性设置为。 trueIf you want to handle the exception by additional event handlers, set the ThrowException property to true.

ColumnIndexRowIndex属性通常指示发生了数据错误的单元格。The ColumnIndex and RowIndex properties 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.

构造函数

DataGridViewDataErrorEventArgs(Exception, Int32, Int32, DataGridViewDataErrorContexts)

初始化 DataGridViewDataErrorEventArgs 类的新实例。Initializes a new instance of the DataGridViewDataErrorEventArgs class.

属性

Cancel

获取或设置指示是否应取消事件的值。Gets or sets a value indicating whether the event should be canceled.

(继承自 CancelEventArgs)
ColumnIndex

获取发生此事件的单元格的列索引。Gets the column index of the cell that the event occurs for.

(继承自 DataGridViewCellCancelEventArgs)
Context

获取有关错误发生时 DataGridView 的状态的详细信息。Gets details about the state of the DataGridView when the error occurred.

Exception

获取表示错误的异常。Gets the exception that represents the error.

RowIndex

获取发生此事件的单元格的行索引。Gets the row index of the cell that the event occurs for.

(继承自 DataGridViewCellCancelEventArgs)
ThrowException

获取或设置值,该值指示是否在 DataGridViewDataErrorEventHandler 委托处理完异常后引发该异常。Gets or sets a value indicating whether to throw the exception after the DataGridViewDataErrorEventHandler delegate is finished with it.

方法

Equals(Object)

确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)

适用于

另请参阅