DataGridViewDataErrorEventArgs::Context Property
Gets details about the state of the DataGridView when the error occurred.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
public: property DataGridViewDataErrorContexts Context { DataGridViewDataErrorContexts get(); }
Property Value
Type: System.Windows.Forms::DataGridViewDataErrorContextsA 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 (dynamic_cast<ConstraintException^>(anError->Exception) != nullptr) { DataGridView^ view = (DataGridView^)sender; view->Rows[anError->RowIndex]->ErrorText = "an error"; view->Rows[anError->RowIndex]->Cells[anError->ColumnIndex]->ErrorText = "an error"; anError->ThrowException = false; } }
Available since 2.0