Back to Devexpress

Row Validation

wpf-6114-controls-and-libraries-data-grid-data-editing-and-validation-input-validation-row-validation.md

latest7.5 KB
Original Source

Row Validation

  • Mar 10, 2025
  • 4 minutes to read

The GridControl supports row validation. Use row validation when data within some columns of a data row are dependent on values in other columns.

The following image shows a GridControl with invalid rows:

Initialize a Validation Command

Use the following API to validate data rows:

|

API

|

Description

| | --- | --- | |

GridViewBase.ValidateRowCommand

TreeListView.ValidateNodeCommand

|

Specifies a command that validates row values.

| |

GridViewBase.ValidateRow

TreeListView.ValidateNode

|

Allows you to validate row values.

|

The grid raises these events or executes commands when saving changes to a data source.

Tip

The grid saves changes in the focused row when a user moves focus to another row or when the CommitEditing method is called.

The following code snippet creates a GridViewBase.ValidateRowCommand to validate rows. The validation command ensures that the Task.StartDate is not greater than the Task.EndDate, and that the Task.TaskName is not empty.

  • args.Item – Specifies a data item that corresponds to the processed data row.

  • args.Result – Specifies the error description.

  • XAML

xaml
<dxg:GridControl ItemsSource="{Binding TaskList}" AutoGenerateColumns="AddNew">
    <dxg:GridControl.View>
        <dxg:TableView AutoWidth="True"
                       ValidateRowCommand="{Binding ValidateRowCommand}"/>
    </dxg:GridControl.View>
</dxg:GridControl>
csharp
using DevExpress.Mvvm;
using DevExpress.Mvvm.DataAnnotations;
using DevExpress.Mvvm.Xpf;

public class MainViewModel : ViewModelBase {
    // ...
    [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("The start date must be before the end date.");
        if(string.IsNullOrEmpty(task.TaskName))
            return new ValidationErrorInfo("The task name cannot be empty.");
        return null;
    }
}
vb
Imports DevExpress.Mvvm
Imports DevExpress.Mvvm.DataAnnotations
Imports DevExpress.Mvvm.Xpf

Public Class MainViewModel
    Inherits ViewModelBase
    ' ...
    <Command>
    Public Sub ValidateRow(ByVal args As RowValidationArgs)
        args.Result = GetValidationErrorInfo(CType(args.Item, Task))
    End Sub

    Private Shared Function GetValidationErrorInfo(ByVal task As Task) As ValidationErrorInfo
        If task.StartDate > task.EndDate Then Return New ValidationErrorInfo("The start date must be before the end date.")
        If String.IsNullOrEmpty(task.TaskName) Then Return New ValidationErrorInfo("The task name cannot be empty.")
        Return Nothing
    End Function

    End Sub
End Class

View Example: Validate Data Rows

Indicate Errors

Error Tooltip

The GridControl displays a tooltip with a message and icon when the user hovers the mouse pointer over an invalid row:

The following code snippet specifies a message and an error icon:

csharp
[Command]
public void ValidateRow(RowValidationArgs args) {
    // ...
    args.Result = new ValidationErrorInfo("Task name cannot be empty.", ValidationErrorType.Critical);
}
vb
<Command>
Public Sub ValidateRow(ByVal args As RowValidationArgs)
    ' ...
    args.Result = New ValidationErrorInfo("Task name cannot be empty.", ValidationErrorType.Critical)
End Sub

Note

You can set the args.Result property to a string. This casts the string to a ValidationErrorInfo object with the specified ErrorContent property and default error icon.

Error Window

The GridControl displays an error window after a user enters an invalid value.

The error window allows users to do the following:

  • Click Yes : Focuses the edited row and allows the user to correct invalid values.
  • Click No : Discards changes. The rollback occurs only if data source objects implement the System.ComponentModel.IEditableObject interface.

Customize Error Behavior

Create a command and bind it to the GridViewBase.InvalidRowExceptionCommand property to customize validation errors. The grid executes this command after the ValidateRowCommand detects errors.

Use the following properties to customize error content and behavior:

PropertyDescription
ExceptionModeSpecifies how the grid reacts to an invalid value. Options include: display a message box (the default setting), throw an exception, display an error icon, or ignore the error.
ErrorTextSpecifies additional text displayed before the “Do you want to correct the value?” prompt in the error window.
WindowCaptionSpecifies the error window’s caption.

The following code snippet creates an InvalidRowCommand that disables the error window:

xaml
<dxg:GridControl ItemsSource="{Binding TaskList}">
    <dxg:GridControl.View>
        <dxg:TableView ValidateRowCommand="{Binding ValidateRowCommand}"
                       InvalidRowExceptionCommand="{Binding InvalidRowCommand}"/>
    </dxg:GridControl.View>
</dxg:GridControl>
csharp
[Command]
public void InvalidRow(InvalidRowExceptionArgs args) {
    args.ExceptionMode = ExceptionMode.NoAction;
}
vb
<Command>
Public Sub InvalidRow(ByVal args As InvalidRowExceptionArgs)
    args.ExceptionMode = ExceptionMode.NoAction
End Sub

See Also

Cell Validation

Interface-Based Validation

Attribute-Based Validation