[Expand]General Information
[Collapse]WinForms Controls
  .NET Core Support
 [Expand]What's Installed
 [Expand]Build an Application
 [Collapse]Controls and Libraries
  [Expand]Forms and User Controls
  [Expand]Messages, Notifications, and Dialogs
  [Expand]Editors and Simple Controls
  [Expand]Ribbon, Bars and Menu
  [Expand]Application UI Manager
  [Expand]Docking Library
  [Expand]Data Grid
  [Collapse]Vertical Grid
   [Expand]Binding to Data
   [Expand]Data Layout: Records, Rows and Cells
   [Collapse]Data Editing and Validation
     In-place Editors
     Assigning Editors to Editor Rows
     Assigning Editors to Multi-Editor Rows
     Showing and Hiding Editors
    Formatting Values
   [Expand]Drag and Drop
   [Expand]Appearance and Custom Painting
   [Expand]Visual Elements
   [Expand]Design-Time Features
   [Expand]End-User Capabilities
    Hit Information
    Batch Modifications
   [Expand]Member Tables
  [Expand]Property Grid
  [Expand]Pivot Grid
  [Expand]Tree List
  [Expand]Gantt Control
  [Expand]Chart Control
  [Expand]Map Control
  [Expand]Rich Text Editor
  [Expand]Spell Checker
  [Expand]Form Layout Managers
  [Expand]Navigation Controls
  [Expand]PDF Viewer
  [Expand]TreeMap Control
  [Expand]Sunburst Control
 [Expand]Common Features
  Get More Help
 [Expand]API Reference
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Expand]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Office File API
[Expand]Report and Dashboard Server
[Expand]eXpressApp Framework
[Expand]eXpress Persistent Objects
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation
View this topic on docs.devexpress.com (Learn more)


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.

Expanded 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).

The VGridControlBase.ValidatingEditor event is not raised if changing cell values via code.

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.

The VGridControlBase.PostEditor and VGridControlBase.CloseEditor methods allow you to initiate validation for the currently active editor. You can get that editor using the VGridControlBase.ActiveEditor property. If validation is a success, these methods will save the current value to the bound data source.

Expanded 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.

Expanded 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.

If the Valid parameter is set to false, the VGridControlBase.InvalidRecordException event is raised after your VGridControlBase.ValidateRecord event handler has been completed. The VGridControlBase.InvalidRecordException event allows you to override the default error presentation (displaying the error message box). It provides an ExceptionMode parameter which can be used to specify how to respond to the error.

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.

Expanded 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.

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.

Expanded See Also

Is this topic helpful?​​​​​​​