ColumnView.InvalidRowException Event
Fires when a row fails validation or when it cannot be saved to the data source.
Namespace: DevExpress.XtraGrid.Views.Base
Assembly: DevExpress.XtraGrid.v24.2.dll
Declaration
Event Data
The InvalidRowException event's data class is InvalidRowExceptionEventArgs. The following properties provide information specific to this event:
Property | Description |
---|---|
ErrorText | Gets or sets the error description to be displayed in the message box/tooltip. Inherited from ExceptionEventArgs. |
Exception | Gets the exception that caused the event. Inherited from ExceptionEventArgs. |
ExceptionMode | Gets or sets the type of response to supplying invalid values. Inherited from ExceptionEventArgs. |
Row | Gets an object which represents the row that failed validation. |
RowHandle | Gets the handle of the row that failed validation. |
WindowCaption | Gets or sets the caption of the error message box. Inherited from ExceptionEventArgs. |
Remarks
If a row was modified and is about to lose focus, the View raises the ColumnView.ValidateRow event. You can handle the event to specify whether row data is valid. If the row fails validation or cannot be saved to the data source due to database restrictions, the InvalidRowException is raised. Note that you can avoid invalid values at the validation stage by leaving the row focused so that end-users can correct the values entered. Another alternative is to discard the changes made.
Handle the InvalidRowException event to provide a proper response to entering invalid data. The row, whose data failed validation, can be identified using the InvalidRowExceptionEventArgs.RowHandle parameter. The exception that raised the event and the error description can be obtained via the ExceptionEventArgs.Exception and ExceptionEventArgs.ErrorText parameters respectively. The actual response is specified by the ExceptionEventArgs.ExceptionMode parameter. You can display an error message box, discard changes made, raise an exception or take no action.
For more information, see the Modify and Validate Cell Values topic.
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;
}