Log In
Home
Support
Demos
Documentation
Blogs
Training
Webinars
[Expand]General Information
[Collapse]WinForms Controls
  Prerequisites
 [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
   [Collapse]Getting Started
    [Collapse]Walkthroughs
     [Expand]Appearance and Conditional Formatting
     [Collapse]Data Editing
       Tutorial: Add or Remove Rows
       Tutorial: Data Input Validation
     [Expand]Hit Information
     [Expand]Data Binding and Working with Columns
     [Expand]Grid View - Columns, Rows and Cells
     [Expand]Banded Views
     [Expand]WinExplorer View
     [Expand]Tile View
     [Expand]Grouping
     [Expand]Filter and Search
     [Expand]Sorting
     [Expand]Summaries
     [Expand]Split Presentation
     [Expand]Row Preview Sections
     Data Grid and Views
   [Expand]Data Binding
   [Expand]Views
   [Expand]Data Editing and Validation
   [Expand]Grouping
   [Expand]Sorting
   [Expand]Filter and Search
   [Expand]Summaries
   [Expand]Focus and Selection Handling
    Formatting Cell Values
   [Expand]Master-Detail Relationships
   [Expand]Asynchronous Image Load
   [Expand]Export and Printing
   [Expand]Appearance and Conditional Formatting
    Split Presentation
    Row Preview Sections
    Scrolling
   [Expand]Batch Modifications
   [Expand]Hit Information
    Hints
   [Expand]Popup Menus
   [Expand]Saving and Restoring Layouts
   [Expand]Visual Elements
   [Expand]Design-Time Features
   [Expand]Examples
   [Expand]End-User Capabilities
    Included Components
  [Expand]Vertical Grid
  [Expand]Pivot Grid
  [Expand]Tree List
  [Expand]Chart Control
  [Expand]Diagrams
  [Expand]Gauges
  [Expand]Map Control
  [Expand]Scheduler
  [Expand]Spreadsheet
  [Expand]Rich Text Editor
  [Expand]Spell Checker
  [Expand]Form Layout Managers
  [Expand]Navigation Controls
  [Expand]Printing-Exporting
  [Expand]PDF Viewer
   Reporting
  [Expand]Snap
  [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]ASP.NET Core Bootstrap Controls
[Expand]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Document Server
[Expand]Reporting
[Expand]Report Server
[Expand]Dashboard
[Expand]eXpressApp Framework
[Expand]CodeRush
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation

Tutorial: Data Input Validation

Expanded Tutorial Video

Expanded Default Data Validation

The grid control has built-in data type validation enabled by default. If you enter a string value into a column bound to a numeric data field, and then, press ENTER or try to move focus away from the cell, the grid validates data input and raises an error as the string cannot be converted to a numeric value. Hover over the error icon to see the same error message.

Correct the entered value or discard changes to continue working with the grid control. To cancel your changes, press ESCAPE.

Expanded Change the Default Error Message

To manually specify the default error message, do the following.

  1. Handle the View's BaseView.ValidatingEditor event.

  2. In the event handler, check the focused column using the GridColumn.FieldName property.

  3. Try converting the event's BaseContainerValidateEditorEventArgs.Value parameter to a required type.

  4. If the value cannot be converted, set the BaseContainerValidateEditorEventArgs.Valid parameter to false and specify your custom BaseContainerValidateEditorEventArgs.ErrorText property value.

Run the application to see how the validation mechanism now shows your custom-defined error message.

Expanded Provide a Custom Data Validation Rule

To provide custom data validation, perform the following steps.

  1. Handle the same BaseView.ValidatingEditor event.

  2. To allow only positive numeric values in a specific column, check that the converted value is greater than 0.

  3. If it is not, set the BaseContainerValidateEditorEventArgs.Valid parameter to false and specify your custom error message.

If you run the application, you will see that the specified error message appears on an attempt to enter zero into a cell.

Expanded Display the Exception Message Box

To customize how errors are displayed, do the following.

  1. Handle the View's BaseView.InvalidValueException event.

  2. In the event handler, show the message box with the ExceptionEventArgs.ErrorText property value.

  3. To suppress the default error icon and tooltip, set the ExceptionEventArgs.ExceptionMode parameter to ExceptionMode.NoAction.

The grid control now informs you about the error using the Message Box.

Expanded Enable Row Validation

If a value's validity depends on other values within the same row, you can enable row validation by handling the ColumnView.ValidateRow event.

For instance, make sure that values in one column are greater than values in another column.

  1. In the ValidateRow event handler, obtain the required column objects.

  2. Use the View's ColumnView.GetRowCellValue method to determine column values in the focused row.

  3. Check if one value is greater than the other.

  4. Set error messages to column cells using the ColumnView.SetColumnError method. Use a column object as the first parameter and error message string as the second parameter.

  5. To assign an error to the entire row, call the ColumnView.SetColumnError method with the first parameter set to null.

  6. Set the event's ValidateRowEventArgs.Valid parameter to false and ValidateRowEventArgs.ErrorText to your custom error message.

Run the application, enter the invalid value and try to switch focus to another row. This is when row validation takes place and the default Error Dialog with the specified error text is invoked.

Click Yes and then hover over error icons to see error messages specified for individual columns.

Expanded Suppress the Default Error Message Box

To disable the default dialog window when validating rows, handle the ColumnView.InvalidRowException event and set the ExceptionEventArgs.ExceptionMode parameter to ExceptionMode.NoAction

The message is now suppressed when entering incorrect values, but you can still see the same error icons in data cells and the row indicator area.

Expanded See Also

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