ColumnView.ValidateRow Event
Enables you to specify whether a modified row’s data is valid, and whether the row can lose focus.
Namespace: DevExpress.XtraGrid.Views.Base
Assembly: DevExpress.XtraGrid.v18.1.dll
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. Inherited from RowEventArgs. |
Valid | Gets or sets whether the row validation succeeds. |
Remarks
The ValidateRow event is raised when a modified row is about to lose focus. Handle this event to specify whether row data is valid, and whether focus movement is allowed.
The inspected row is identified by the RowEventArgs.RowHandle parameter. You can pass this parameter value to the ColumnView.GetRowCellValue and ColumnView.GetRowCellDisplayText methods to obtain cell values and their formatted representation. After row values have been obtained, analyze them, to determine whether row data is valid. If not, set the ValidateRowEventArgs.Valid parameter to false. This will result in displaying an error message whose text is specified by the ValidateRowEventArgs.ErrorText parameter.
Clicking the Yes button returns focus to the row. This allows end-users to correct the values entered. If the No button is clicked, row focus is moved and the previously focused row’s data changes are discarded. Note that the rollback only occurs if objects that represent records implement the IEditableObject interface.
If row validation fails, you can indicate the cell containing an invalid value. Call the ColumnView.SetColumnError method for this purpose. The method takes the column object and error description text as parameters. As the result, the cell within the specified column displays an error icon (). Pointing to the icon invokes the hint with the appropriate error description. Please refer to the Internal ErrorInfo Support topic, for additional information.
Note that the result of handling the ValidateRow event can be overridden by handling the ColumnView.InvalidRowException event. Please refer to the Modify and Validate Cell Values topic, for additional information.
Example
Assume that a Grid View contains two columns: “Units In Stock” and “Units On Order”. A value in the first column must be more than the value of the second one. So we need to perform validation of a row when it is about to be saved to the data source. For this purpose, the ColumnView.ValidateRow event is handled.
If row fails validation, we set errors for the columns with coresponding descriptions using the ColumnView.SetColumnError method. The descriptions will be displayed when hovering over error icons.
The ColumnView.InvalidRowException event is handled in order to suppress displaying the default error message box.
The following screenshot shows a Grid View after a row fails validation.
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");
}
}
private void gridView1_InvalidRowException(object sender,
DevExpress.XtraGrid.Views.Base.InvalidRowExceptionEventArgs e) {
//Suppress displaying the error message box
e.ExceptionMode = ExceptionMode.NoAction;
}
Related GitHub Examples
The following code snippets (auto-collected from DevExpress Examples) contain references to the ValidateRow event.
Note
The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.