Skip to main content

Row Validation

  • 4 minutes to read

The GridControl allows you to validate row values.

Use the row validation if the row’s data is interrelated (for example, values in some columns depend on other column values). Refer to the following help topic for more information on how to validate cell values: Cell Validation.

The image below shows a GridControl with an invalid row:

DevExpress WPF | Grid Control - Row Validation MVVM

View Example: How to Validate Data Rows

Initialize a Validation Command

The GridControl contains the following API members that allow you to validate row values:

API Description
GridViewBase.ValidateRowCommand / TreeListView.ValidateNodeCommand Gets or sets a command that validates row values.
GridViewBase.ValidateRow / TreeListView.ValidateNode Allows you to validate row values.

The GridControl raises these events / executes commands when the control saves changes to a data source. The save changes process starts when a user focuses another row, or you call the DataViewBase.CommitEditing method.

The code sample below shows how to use the GridViewBase.ValidateRowCommand property to validate rows:

<dxg:GridControl ItemsSource="{Binding TaskList}" AutoGenerateColumns="AddNew">
    <dxg:GridControl.View>
        <dxg:TableView AutoWidth="True"
                       ValidateRowCommand="{Binding ValidateRowCommand}"/>
    </dxg:GridControl.View>
</dxg:GridControl>
using DevExpress.Mvvm;
using DevExpress.Mvvm.DataAnnotations;
using DevExpress.Mvvm.Xpf;
// ...
public class MainViewModel : ViewModelBase {
    // ...
    [Command]
    public void ValidateRow(RowValidationArgs args) { }
}

Validate a New Value

The Item property returns a data item within the processed row. To indicate that the row values are invalid, set the Result property to an error description.

In the following code sample, StartDate cannot be greater than EndDate and TaskName cannot be empty:

[Command]
public void ValidateRow(RowValidationArgs args) {
    args.Result = GetValidationErrorInfo((Task)args.Item);
}
static ValidationErrorInfo GetValidationErrorInfo(Task task) {
    if(task.StartDate > task.EndDate)
        return new ValidationErrorInfo("Start Date must be less than End Date");
    if(string.IsNullOrEmpty(task.TaskName))
        return new ValidationErrorInfo("Enter a task name");
    return null;
}

Indicate Errors

An Error Tooltip

The GridControl shows an error tooltip when the mouse pointer hovers over an invalid row. This tooltip contains an error description and an error icon:

DevExpress WPF | Grid Control - Row Validation Error Tooltip

The Result property accepts objects of the ValidationErrorInfo type. In this object, you can specify an error description and error icon.

[Command]
public void ValidateRow(RowValidationArgs args) {
    // ...
    args.Result = new ValidationErrorInfo("Enter a task name", ValidationErrorType.Critical);
}

If you assign an error description string to the Result property, it casts this string to a ValidationErrorInfo object with the default error icon.

An Error Window

The GridControl displays an error window after a user enters an invalid 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.

Row Validation Error Window

Customize an Error Behavior

Create a command and bind it to the GridViewBase.InvalidRowExceptionCommand property to specify how to display validation errors. The GridControl executes this command after the command bound to the GridViewBase.ValidateRowCommand property indicates errors. You can use the following InvalidRowExceptionArgs properties to override the default error behavior:

  • The ExceptionMode property allows you to specify which action to perform when a user enters an invalid value. You can display a message box with an error description (default behavior), throw an exception, display an error icon, or ignore this exception.
  • The ErrorText property allows you to specify an additional error description that is displayed in the error window before the “Do you want to correct the value?” string.
  • The WindowCaption property allows you to change the error window’s caption.

The code sample below demonstrates how to disable the error window and display the error tooltip only:

<dxg:GridControl ItemsSource="{Binding TaskList}">
    <!-- ... -->
    <dxg:GridControl.View>
        <dxg:TableView ValidateRowCommand="{Binding ValidateRowCommand}"
                       InvalidRowExceptionCommand="{Binding InvalidRowCommand}"/>
    </dxg:GridControl.View>
</dxg:GridControl>
[Command]
public void InvalidRow(InvalidRowExceptionArgs args) {
    args.ExceptionMode = ExceptionMode.NoAction;
}