Edit

Share via


DataGridViewDataErrorEventArgs Class

Definition

Provides data for the DataError event.

public class DataGridViewDataErrorEventArgs : System.Windows.Forms.DataGridViewCellCancelEventArgs
Inheritance

Examples

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;
    }
}

Remarks

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.

Constructors

Properties

Cancel

Gets or sets a value indicating whether the event should be canceled.

(Inherited from CancelEventArgs)
ColumnIndex

Gets the column index of the cell that the event occurs for.

(Inherited from DataGridViewCellCancelEventArgs)
Context

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.

(Inherited from DataGridViewCellCancelEventArgs)
ThrowException

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

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to

Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

See also