DataGridViewDataErrorEventArgs.Context Property

 

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

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

public DataGridViewDataErrorContexts Context { get; }

Property Value

Type: System.Windows.Forms.DataGridViewDataErrorContexts

A bitwise combination of the DataGridViewDataErrorContexts values that specifies the context in which the error occurred.

Members of the DataGridViewDataErrorContexts enumeration may be combined using the bitwise OR operator to represent the state of a data-bound DataGridView when a data error occurred. For example, if a user enters an invalid cell value (such as entering a name in a cell that requires a date) and then selects a different cell, the DataGridView will try to commit the invalid cell value. When the commit fails, the DataGridView will raise a DataError event whose Context property will have a value of Commit and CurrentCellChange.

The following code example demonstrates how to investigate the error context. 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
Return to top
Show: