Export (0) Print
Expand All

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

Supported in: 4.5.2, 4.5.1, 4.5, 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.

Show:
© 2014 Microsoft