VGridControlBase.ValidateRecord Event
Enables you to specify whether record data is valid and whether the record can lose focus.
Namespace: DevExpress.XtraVerticalGrid
Assembly: DevExpress.XtraVerticalGrid.v24.2.dll
Declaration
Event Data
The ValidateRecord event's data class is DevExpress.XtraVerticalGrid.Events.ValidateRecordEventArgs.
Remarks
The ValidateRecord event allows you to perform record validation in a VGridControl. The PropertyGridControl doesn’t support record validation.
The ValidateRecord event is raised when a record is about to be saved to a data source, as a result of either moving focus to another record or saving the record using the VGridControlBase.UpdateFocusedRecord method. Handle this event to specify whether record data is valid and whether focus movement or saving is allowed.
The processed record is identified by the event parameter’s RecordIndex property. You can pass this property’s value to the VGridControlBase.GetCellValue or VGridControlBase.GetCellDisplayText method to obtain the cell’s value and its formatted representation.
After record values have been obtained, analyze them to determine whether the record’s data is valid. If not, set the Valid property to false. This will result in an error message being displayed. The text of this message is specified by the event parameter’s ErrorText property.
Clicking the Yes button returns focus to the record. This allows end-users to correct the values entered. If the No button has been clicked, record focus is moved and the changes are discarded.
If record validation fails, you can indicate the cell which contains an invalid value. To do this, call the VGridControlBase.SetRowError method. As a result, the cell whose value is invalid displays an error icon (). Pointing to the icon displays a hint with the error description.
Note
The ValidateRecord event is not supported in Unbound Mode.
Example
Assume that the vertical grid contains two rows: “OrderDate” and “RequiredDate”. The value in the first row must be less than the value in the second one. So we need to validate their values in a record when it is about to be saved to the data source. For this purpose, the VGridControlBase.ValidateRecord
event is handled.
If the record fails validation, we set errors for the rows with corresponding descriptions using the VGridControlBase.SetRowError method. The descriptions will be displayed when the mouse cursor hovers over the error icons.
The VGridControlBase.InvalidRecordException event is handled in order to suppress displaying the default error message box.
The following image shows a vertical grid after a record fails validation.
using DevExpress.XtraVerticalGrid.Events;
private void vGridControl1_ValidateRecord(object sender, ValidateRecordEventArgs e) {
VGridControl vGrid = sender as VGridControl;
// Get the value of the first row.
DateTime value1 = Convert.ToDateTime(vGrid.GetCellValue(editorRow2,
e.RecordIndex));
// Get the value of the second row.
DateTime value2 = Convert.ToDateTime(vGrid.GetCellValue(editorRow3,
e.RecordIndex));
if (value1 >= value2) {
e.Valid = false;
// Set errors with specific descriptions for the rows.
vGrid.SetRowError(editorRow2.Properties,
"The value must be less than Required Date");
vGrid.SetRowError(editorRow3.Properties,
"The value must be greater than Order Date");
}
}
private void vGridControl1_InvalidRecordException(object sender,
InvalidRecordExceptionEventArgs e) {
// Suppress displaying the error message box.
e.ExceptionMode = DevExpress.XtraEditors.Controls.ExceptionMode.NoAction;
}