Skip to main content
A newer version of this page is available. .

How to: Validate Data Entered by End-Users

  • 2 minutes to read

The following example prohibits invalid “colBudget” column cell value assignment. The cell value should be grater than zero and less than 1,000,000. The BaseView.ValidatingEditor event is handled to check the entered value’s validity. The BaseView.InvalidValueException event is handled to display an exception message box if invalid cell value assignment occurs. In this instance, the GridView.HideEditor method is called to discard the changes made and to destroy the cell’s editor.

using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;

private void gridView1_ValidatingEditor(object sender, BaseContainerValidateEditorEventArgs e) {
    ColumnView view = sender as ColumnView;
    GridColumn column = (e as EditFormValidateEditorEventArgs)?.Column ?? view.FocusedColumn;
    if (column.Name != "colBudget") return;
    if ((Convert.ToInt32(e.Value) < 0) || (Convert.ToInt32(e.Value) > 1000000))
        e.Valid = false;
}

private void gridView1_InvalidValueException(object sender, InvalidValueExceptionEventArgs e) {
    ColumnView view = sender as ColumnView;
    if (view == null) return;
    e.ExceptionMode = ExceptionMode.DisplayError;
    e.WindowCaption = "Input Error";
    e.ErrorText = "The value should be greater than 0 and less than 1,000,000";
    // Destroy the editor and discard the changes made within the edited cell.
    view.HideEditor();
}