Skip to main content

Validate Data

  • 3 minutes to read

Cell Validation

When a user modifies a focused cell’s value and tries to save changes (presses the ENTER key or moves focus to another cell), the GridControl validates a new value. First, this value is validated by the editor itself. Then, the GridControl raises the ColumnBase.Validate and GridControl.ValidateCell events. These events allow you to validate the cell’s value. The GridControl does not raise these events when you change cell values in code.

Do the following to validate data in a specific column:

  1. Handle the column’s ColumnBase.Validate event.

  2. Use the e.Value property to obtain the input value that should be validated. If a cell’s value fails validation, set the e.IsValid property to false. In this case, a user cannot move focus to another cell until the cell’s value is valid. To disable this behavior, set the AllowLeaveInvalidEditor property to true.

  3. Use the e.ErrorType and e.ErrorContent properties to specify an icon and an error description.

<dxg:GridControl ItemsSource="{x:Bind ViewModel.Source}" 
                 AutoGenerateColumns="False" 
                 NavigationStyle="Cell" 
                 AllowLeaveInvalidEditor="True">
    <dxg:GridControl.Columns>
        <dxg:GridTextColumn FieldName="ProductName" Validate="GridTextColumn_Validate"/>
        <dxg:GridTextColumn FieldName="Country"/>
        <dxg:GridTextColumn FieldName="City"/>
        <dxg:GridSpinEditColumn FieldName="UnitPrice"/>
        <dxg:GridTextColumn FieldName="Quantity" />
    </dxg:GridControl.Columns>
</dxg:GridControl>
void GridTextColumn_Validate(object sender, GridCellValidationEventArgs e) {
    e.IsValid = (e.Value != null) && (e.Value.ToString().Length >= 3);
    e.ErrorType = DevExpress.WinUI.Editors.ErrorType.Warning;
    e.ErrorContent = "The product name must contain at least 3 characters.";
}

Row Validation

You can check the validity of data entered by users into a data row. This is useful when the integrity of row data is important (for instance, when values in some columns depend on values in others).

A row is validated in the GridControl.ValidateRow event handler when it is about to lose focus. To manually force row validation, call the DataControlBase.CommitEditing method.

The processed row is returned by the event parameter’s Row property. Its handle is returned by the RowHandle property. After cell values have been obtained, you can verify whether the values meet your validity criteria. If the row fails validation, set the event’s IsValid parameter to false.

Grid Validation Error

<dxg:GridControl ItemsSource="{x:Bind ViewModel.Source}" 
                 AutoGenerateColumns="True" 
                 NavigationStyle="Cell" 
                 ValidateRow="GridControl_ValidateRow"/>
void GridControl_ValidateRow(object sender, GridRowValidationEventArgs e) {
    e.Handled = true;
    e.IsValid = ((Product)e.Row).OrderDate < ((Product)e.Row).RequiredDate;
    if (!e.IsValid)
        e.ErrorContent = "\"Order Date\" must precede \"Required Date\"";
}

Customize the Error Window

The GridControl displays an error window after a user enters an incorrect value. This window allows them to perform the following actions:

  • Click Yes to return focus to the edited row and correct the row value.
  • Click No to discard the changes. In this case, a user can move the focus away from the row. The rollback only occurs if data source objects implement the System.ComponentModel.IEditableObject interface.

Handle the GridControl.InvalidRowException event, which is raised after the GridControl.ValidateRow event, and use the following InvalidRowExceptionEventArgs‘s properties to override the default error behavior:

The code sample below disables the error window:

<dxg:GridControl ItemsSource="{x:Bind ViewModel.Source}" 
                 AutoGenerateColumns="True" 
                 NavigationStyle="Cell" 
                 ValidateRow="GridControl_ValidateRow" 
                 InvalidRowException="GridControl_InvalidRowException"/>
void GridControl_InvalidRowException(object sender, InvalidRowExceptionEventArgs e) {
    e.ExceptionMode = ExceptionMode.NoAction;
}