DataGridViewDataErrorEventArgs Class
Collapse the table of content
Expand the table of content

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

System_CAPS_pubmethodDataGridViewDataErrorEventArgs(Exception, Int32, Int32, DataGridViewDataErrorContexts)

Initializes a new instance of the DataGridViewDataErrorEventArgs class.


Gets or sets a value indicating whether the event should be canceled.(Inherited from CancelEventArgs.)


Gets the column index of the cell that the event occurs for.(Inherited from DataGridViewCellCancelEventArgs.)


Gets details about the state of the DataGridView when the error occurred.


Gets the exception that represents the error.


Gets the row index of the cell that the event occurs for.(Inherited from DataGridViewCellCancelEventArgs.)


Gets or sets a value indicating whether to throw the exception after the DataGridViewDataErrorEventHandler delegate is finished with it.


Determines whether the specified object is equal to the current object.(Inherited from Object.)


Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)


Serves as the default hash function. (Inherited from Object.)


Gets the Type of the current instance.(Inherited from Object.)


Creates a shallow copy of the current Object.(Inherited from Object.)


Returns a string that represents the current object.(Inherited from Object.)

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.

Legacy Code Example

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
Available since 2.0

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

Return to top
© 2015 Microsoft