Export (0) Print
Expand All

DataGridViewDataErrorEventArgs Class

Provides data for the DataError event.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public class DataGridViewDataErrorEventArgs : DataGridViewCellCancelEventArgs

The DataGridViewDataErrorEventArgs type exposes the following members.

  NameDescription
Public methodDataGridViewDataErrorEventArgsInitializes a new instance of the DataGridViewDataErrorEventArgs class.
Top

  NameDescription
Public propertyCancelGets or sets a value indicating whether the event should be canceled. (Inherited from CancelEventArgs.)
Public propertyColumnIndexGets the column index of the cell that the event occurs for. (Inherited from DataGridViewCellCancelEventArgs.)
Public propertyContextGets details about the state of the DataGridView when the error occurred.
Public propertyExceptionGets the exception that represents the error.
Public propertyRowIndexGets the row index of the cell that the event occurs for. (Inherited from DataGridViewCellCancelEventArgs.)
Public propertyThrowExceptionGets or sets a value indicating whether to throw the exception after the DataGridViewDataErrorEventHandler delegate is finished with it.
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

Handling the DataError event lets you handle exceptions thrown by code outside your control (for example, by an external data source). Use the Context property to determine the state of the DataGridView at the time of the exception. Use the Exception property to retrieve the exception data. If you want to handle the exception by additional event handlers, set the ThrowException property to true.

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. In this case, the ColumnIndex property typically indicates the column of the current cell at the time of the error.

The following code example demonstrates how to respond to information provided by the DataGridViewDataErrorEventArgs class. 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 ((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;
    }
}

.NET Framework

Supported in: 4.5.3, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft