Validation

The vertical grids (VGridControl and PropertyGridControl) support the validation mechanism that allows the data being entered by end-users to be checked. If the new values violate specific restrictions you can indicate the error(s).
You can implement validation rules for individual cells and/or records. Record validation is only supported for the VGridControl.

It's also possible to implement a validation mechanism for in-place editors. However, this technique is mostly applicable to standalone editors. See the Validation topic for more information.

Validating cells

This method allows you to impose specific restrictions on individual cells. If invalid data is entered you can prevent an end-user from leaving the cell until data is corrected.

When a cell's value is modified and an end-user tries to leave this cell, the VGridControlBase.ValidatingEditor event fires.
You can handle this event to provide your validation criteria. For instance, you can check whether the entered value is greater than a specific value, that it falls within a particular range, belongs to some value list, etc. If the cell's value meets the custom criteria, set the event's Valid parameter to true. Otherwise, you can set it to false. The initial value of the Valid parameter is determined by the result of the automatic editor's validation performed by the in-place editor's BaseEdit.DoValidate method. Note that if the parameter is set to false, the cell remains focused by default. This forces the end-user to correct the value entered.

The value being validated can be accessed via the event's Value parameter. If the value does not meet your criteria, you can correct the value manually right within your VGridControlBase.ValidatingEditor event handler. For this purpose, you should assign the desired value to the Value parameter. Set the Valid parameter to true to allow focus to be moved from the cell. Or you can set the parameter to false in order to further process the issue via the VGridControlBase.InvalidValueException event (see below).

If the Valid parameter is set to false, the grid will display an error icon () within the cell. When hovering over the error icon a descriptive hint ("Invalid Value") is displayed. To provide a custom error description, assign the desired string to the event's ErrorText parameter.

If you need to prevent error icons from being displayed within cells or you need to display a message box instead of the error icons, you can handle the VGridControlBase.InvalidValueException event. This event fires in all cases when the entered (or assigned via code) value cannot be accepted by the control or underlying data source.

In some cases you may want to display error icons for several cells at once. This lets you indicate that the current cell value conflicts with other cells and the end-user has to correct values in one or more of these cells. To display an error icon for a cell, call the VGridControlBase.SetRowError method. The string to be displayed within the error tooltip is passed to this method as a parameter.

Example - Validating cells

The following example prohibits assignment of invalid values to "Department Budget" child rows. The cell's value should be no greater than 100,000. The VGridControlBase.ValidatingEditor event is handled to check the entered value's validity. The VGridControlBase.InvalidValueException event is handled to display an exception message box if an invalid value is assigned. In this instance, the VGridControlBase.HideEditor method is called to discard the changes made and destroy the cell's editor.

Validating records

In the VGridControl, you can implement the validation procedure for entire records (not for individual cells). In this case, an end-user is not able to move away from the current record unless the data is correct but it's possible to freely move focus between cells within the record.

To check the validity of data in records, handle the VGridControlBase.ValidateRecord event. This event fires when the current record has been modified and it's about to be saved to the underlying data source. For instance, this takes place when an end-user makes an attempt to move focus to another record or when the VGridControlBase.UpdateFocusedRecord method is called.

The event's RecordIndex parameter allows you to identify the record being processed. To obtain cell values, you can use the VGridControlBase.GetCellValue method. After cell values have been obtained, you can verify whether the values meet your validity criteria. If a record fails validation, set the event's Valid parameter to false. Otherwise, leave the Valid parameter set to true.

By default, if the Valid parameter is set to false, the grid displays an error message box:

Clicking the Yes button returns focus to the record. This allows the end-user to correct the record's values. If the No button is clicked, the record's changes are discarded and focus can be moved away from the record. Note that the rollback only occurs if objects that represent records implement the System.ComponentModel.IEditableObject interface.

It is possible to provide an additional error description via the event's ErrorText parameter. The string assigned to this parameter will be displayed in the error message box followed by the "Do you want to correct the value?" string.

Sometimes you may want to indicate the cells contain invalid data to the end-user. For that purpose, call the VGridControlBase.SetRowError method with the column and error description as parameters. As a result, the corresponding cell displays an error icon (). Pointing to the icon invokes a hint with the specified error description.

The VGridControlBase.InvalidRecordException event also occurs when the validated record cannot be saved to the bound data source due to database restrictions. In this case, an exception is usually raised by the data source.

Example - Validating records

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.