Log In
[Expand]General Information
[Collapse]WinForms Controls
 [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
  [Collapse]Data Grid
   [Expand]Getting Started
   [Expand]Binding to Data
   [Expand]Grid View
   [Expand]View Technology
   [Collapse]Data Editing
    [Expand]Edit Form
    [Expand]Editing via Code
    [Expand]In-place Editors
    [Collapse]Input Validation
     [Expand]ErrorInfo Support
      Validating Editors
      Validating Rows
    [Expand]New Item Row
     Tutorial: Add or Remove Rows
     Tutorial: Data Input Validation
   [Expand]Filtering and Locating Rows
   [Expand]Focus and Selection Handling
   [Expand]Processing Rows
   [Expand]Formatting Cell Values
   [Expand]Master-Detail Relationships
   [Expand]Asynchronous Image Load
   [Expand]Export and Printing
   [Expand]Appearance and Conditional Formatting
   [Expand]Batch Modifications
   [Expand]Hit Information
   [Expand]Popup Menus
   [Expand]Saving and Restoring Layouts
   [Expand]Visual Elements
   [Expand]Design-Time Features
   [Expand]End-User Capabilities
    Included Components
  [Expand]Vertical Grid
  [Expand]Pivot Grid
  [Expand]Tree List
  [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]Common Features
  Get More Help
 [Expand]API Reference
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Document Server
[Expand]Report Server
[Expand]eXpressApp Framework
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation

Validating Editors

The DevExpress WinForms Grid (XtraGrid) control enables you to check the validity of data entered by end-users before posting this data to the underlying data source. For this purpose, the control raises specific events for validating modified cells/editors and rows. Such events allow you to prohibit saving incorrect data to the bound data source, and indicate the reason for the error to the end-user. This document provides information related to validating cells/editors. For information on validating rows, refer to the Validating Rows section.

Expanded Online Video

This video will first show you the built-in data validation mechanisms enabled in the Grid Control by default. You will then learn how to specify your own data cell validation rules and how to control the UI that informs end-users about the errors. Finally, you'll see how to enable row validation, which allows you to take into account values from multiple columns.

Expanded General Concepts

When an end-user modifies a value in an embedded editor and tries to save the changes (for instance, by pressing the ENTER key or by moving focus to another cell), the grid control performs editor validation. First, the value is validated automatically by the editor itself. Then, the BaseView.ValidatingEditor event is raised, which allows you to control the editor's value validity. You can handle this event to provide your validation criteria. For instance, you can check to see if the entered value is greater than a specific value, it falls within a particular range, belongs to some value list, etc. If an editor 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. Note that if the parameter is set to false, the cell remains focused by default. This forces the 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 within the BaseView.ValidatingEditor event handler. For this purpose, assign the desired value to the Value parameter. Set the Valid parameter to true, to allow focus to be moved from the editor. Or you can set the parameter to false to further process the issue via the BaseView.InvalidValueException event (see below).

The BaseView.ValidatingEditor event is not raised when changing cell values via code.

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

Views allow you to change the default error presentation manner. For this purpose, handle the BaseView.InvalidValueException event. It is raised right after the BaseView.ValidatingEditor event if its Valid parameter has been set to false. The BaseView.InvalidValueException event provides a number of parameters controlling error presentation. Use the ExceptionMode parameter to specify how to respond to the validation error that has occurred.

ExceptionMode value Action
DisplayError Displays an error icon with a tooltip.
Ignore Discards new data and reverts to an old value.
NoAction Suppresses displaying the tooltip or throwing an exception. The value remains unchanged.
ThrowException Throws an exception.

Note that the BaseView.InvalidValueException event may also occur when changing a cell value via code (for instance, if the value is of the wrong type).

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 must correct the values in one or more of these cells. To display an error icon for a cell, call the ColumnView.SetColumnError method. The string to be displayed within the error tooltip is passed to this method as a parameter.

The BaseView.ValidateEditor and BaseView.PostEditor methods allow you to initiate validation for the currently active editor. You can get that editor using the BaseView.ActiveEditor property. The BaseView.ValidateEditor method simply fires the validation events. The BaseView.PostEditor method calls BaseView.ValidateEditor and, if validation is a success, saves the value to the bound data source.

Expanded Identifying Current Row and Column

Depending on the GridOptionsBehavior.EditingMode property, data in the GridView can be edited in-place (directly within cells) or using an Edit Form (a form with standalone editors, which can be opened inline or as a separate modal window). Other Views only support in-place editing. The BaseView.ValidatingEditor event can be used for data validation in both of these modes.

While handling the BaseView.ValidatingEditor event, you may need to identify the current row and column. The row can be identified using the ColumnView.FocusedRowHandle parameter. The currently processed column is identified differently depending on the data editing mode:

Expanded Example

The following code shows how to handle the BaseView.ValidatingEditor event to restrict a user's input for the Discount column. The column only accepts values between 0 and 0.20. Other values are considered invalid. The BaseView.InvalidValueException event is handled to display a message box with a custom error message.

Expanded See Also

How would you rate this topic?​​​​​​​