Internal ErrorInfo Support
- 5 minutes to read
Grid Control provides methods with which you can get and set errors for any cell within a focused record and for the entire focused record. For instance, you may verify the focused record, and if invalid data is encountered, set errors for particular cells. The grid control will indicate the cells and the record, for which errors are set, by displaying error icons.
This section describes the methods provided by Grid Control for that purpose.
Setting errors for the focused record/record cells can be useful, for instance, when validating the user’s input. Grid Control allows you to validate data the end-user enters via the BaseView.ValidatingEditor and ColumnView.ValidateRow events. When handling these events, you can set errors for specific cells or for the entire record to indicate invalid data. For more information on validating, see the Modify and Validate Cell Values and Modify and Validate Cell Values sections.
Internal ErrorInfo Support
To indicate that particular cells of the focused record contain invalid data, you can call the ColumnView.SetColumnError method. This allows you to set an error description for a specific column or for the entire record. It is also possible to specify the type of error using the ColumnView.SetColumnError method overload, which takes an ErrorType parameter. The error type identifies the error icon to be displayed within the cell. The following predefined error icons are available: , and . In addition, you can provide custom error icons.
If an error description is set for a column, an error icon is displayed within a corresponding cell. If you set an error description for a record, the error icon is displayed in the row indicator‘s cell (for Grid Views) and within the card caption (for Card Views). The following images illustrate errors that are set for cells and records for a Grid View and Card View respectively.
GridView | CardView |
---|---|
If you need to provide a more complex error presentation, you can use the custom draw and Appearance techniques. For instance, you may want to indicate the error by displaying custom information within the column header which contains an invalid value. For this purpose, you can handle the GridView.CustomDrawColumnHeader event.
To get and clear column and record errors, use the following methods:
ColumnView.ClearColumnErrors removes error descriptions for the entire focused record and for its cells. To clear error descriptions for particular cells or for the record only, call the ColumnView.SetColumnError method with the errorText parameter set to an empty string.
The grid control automatically calls the ColumnView.ClearColumnErrors method after the row validation succeeds.
- ColumnView.GetColumnError and ColumnView.GetColumnErrorType return the error description and error type that have been set for a particular cell or for the entire focused record.
- ColumnView.HasColumnErrors checks on whether the record or record cells have errors assigned.
All error related methods provided by Grid Control work with the focused row. However, if the view is bound to a DataTable or DataView data source, it is possible to set and get errors for cells in any row using methods of the corresponding DataRow object (SetColumnError and GetColumnError). The grid will indicate cells containing invalid data with error icons. The error related methods provided by Grid Control do not allow you to work with error descriptions which are set via the data source.
To implement error support for other data sources, you need to implement the IDataErrorInfo or IDXDataErrorInfo interface. See the Implementing ErrorInfo Support for Data Sources section for an example.
Example
The example below tracks changes made to the “Units In Stock” and “Units On Order” columns. If an edited record has its “In Stock” value less than “On Order”, this row is considered invalid. In this case the e.Valid parameter of the ColumnView.ValidateRow event is set to false.
The default warning message does not pop up since the ExceptionMode parameter of the ColumnView.InvalidRowException event is set to NoAction. Instead, the ColumnView.SetColumnError method displays error icons in both cells.
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraEditors.Controls;
private void gridView1_ValidateRow(object sender,
DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e) {
GridView view = sender as GridView;
GridColumn inStockCol = view.Columns["UnitsInStock"];
GridColumn onOrderCol = view.Columns["UnitsOnOrder"];
//Get the value of the first column
Int16 inSt = (Int16)view.GetRowCellValue(e.RowHandle, inStockCol);
//Get the value of the second column
Int16 onOrd = (Int16)view.GetRowCellValue(e.RowHandle, onOrderCol);
//Validity criterion
if (inSt < onOrd) {
e.Valid = false;
//Set errors with specific descriptions for the columns
view.SetColumnError(inStockCol, "The value must be greater than Units On Order");
view.SetColumnError(onOrderCol, "The value must be less than Units In Stock");
}
if(e.Valid)
view.ClearColumnErrors();
}
private void gridView1_InvalidRowException(object sender,
DevExpress.XtraGrid.Views.Base.InvalidRowExceptionEventArgs e) {
//Suppress displaying the error message box
e.ExceptionMode = ExceptionMode.NoAction;
}