ColumnView.ValidateRow Event
Fires when a user edits row cell values and attempts to select another row. Handle this event to check whether these new values are valid, and if not, choose the required behavior (discard edits, show a warning message, ignore errors, or keep the focus on this row until a user enters valid values).
Namespace: DevExpress.XtraGrid.Views.Base
Assembly: DevExpress.XtraGrid.v24.1.dll
NuGet Packages: DevExpress.Win.Grid, DevExpress.Win.Navigation
Declaration
Event Data
The ValidateRow event's data class is ValidateRowEventArgs. The following properties provide information specific to this event:
Property | Description |
---|---|
ErrorText | Gets or sets the error description. |
Row | Gets the processed row. Inherited from RowObjectEventArgs. |
RowHandle | Gets the row’s handle (position). For the ColumnView.RowUpdated event, this property specifies the previous handle (position) of the currently processed row. NewItemRowHandle value when a new row is added. Inherited from RowEventArgs. |
Valid | Gets or sets whether the row validation succeeds. |
Remarks
When a user modifies cell values and attempts to move focus to another row, the ValidateRow event fires. Handle this event to review all changes (use the ColumnView.GetRowCellValue and ColumnView.GetRowCellDisplayText method to do that). If any cell value is incorrect, set the e.Valid parameter to false. The following behavior depends on whether you have handled the ColumnView.InvalidRowException event, and if so, what value is assigned to its e.ExceptionMode parameter.
If the InvalidRowException event was not handled (or its ExceptionMode equals DisplayError), Data Grid keeps a user on this row and shows a notification message. The ValidateRowEventArgs.ErrorText parameter allows you to modify this message’s text. A user can click “No” to dismiss this warning and move to another row, but all edits made to the current row cells will be lost. Note that Data Grid can rollback cell values only when data source entities implement the IEditableObject interface.
Instead of validating an entire row, you can handle the BaseView.ValidatingEditor and BaseView.InvalidValueException events to validate every row cell separately.
See the Modify and Validate Cell Values and Internal ErrorInfo Support articles for more information on cell and row validation.
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;
}