Skip to main content

GridViewBase.ValidateRow Event

Allows you to validate row values.

Namespace: DevExpress.Xpf.Grid

Assembly: DevExpress.Xpf.Grid.v24.2.dll

NuGet Package: DevExpress.Wpf.Grid.Core

Declaration

public event GridRowValidationEventHandler ValidateRow

Event Data

The ValidateRow event's data class is GridRowValidationEventArgs. The following properties provide information specific to this event:

Property Description
CancellationToken Gets an object that notifies that the Update operation should be canceled.
Culture Gets the culture related to the validation. Inherited from ValidationEventArgs.
ErrorContent Gets or sets an object that describes the validation error. Inherited from ValidationEventArgs.
ErrorType Gets or sets the error icon type. Inherited from ValidationEventArgs.
Handled Gets or sets a value that indicates the present state of the event handling for a routed event as it travels the route. Inherited from RoutedEventArgs.
IsNewItem
IsValid Gets or sets a value specifying whether the value is valid. Inherited from ValidationEventArgs.
OriginalSource Gets the original reporting source as determined by pure hit testing, before any possible Source adjustment by a parent class. Inherited from RoutedEventArgs.
RoutedEvent Gets or sets the RoutedEvent associated with this RoutedEventArgs instance. Inherited from RoutedEventArgs.
Row Gets the processed row.
RowHandle Gets the processed row’s handle.
Source Gets or sets a reference to the object that raised the event. Inherited from RoutedEventArgs.
UpdateRowResult Gets or sets a task that allows you to asynchronously post changes to an underlying data source (database).
UpdateSource Gets the action that caused the validation. Inherited from ValidationEventArgs.
UseCancellationToken Gets or sets whether the Update operation can be canceled.
Value Gets the editor’s value. Inherited from ValidationEventArgs.

The event data class exposes the following methods:

Method Description
InvokeEventHandler(Delegate, Object) When overridden in a derived class, provides a way to invoke event handlers in a type-specific way, which can increase efficiency over the base implementation. Inherited from RoutedEventArgs.
OnSetSource(Object) When overridden in a derived class, provides a notification callback entry point whenever the value of the Source property of an instance changes. Inherited from RoutedEventArgs.
SetError(Object, ErrorType) Marks the processed input value as invalid and displays an error within the editor with the specified error icon type. Inherited from ValidationEventArgs.
SetError(Object) Marks the processed input value as invalid and displays an error within the editor. Inherited from ValidationEventArgs.

Remarks

The GridControl raises the ValidateRow event 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 Row and RowHandle properties return the processed row and the row’s handle, respectively.

You can obtain cell values and verify if they meet your validation criteria. If these values are incorrect, set the IsValid property to false.

Refer to the following topic for more information: Row Validation.

If you want to maintain a clean MVVM pattern and process the row validate operation in a View Model, create a command and bind it to the ValidateRowCommand property.

Use the UpdateRowResult property to validate row data asynchronously.

CRUD Operations

You can implement CRUD operations (create, read update, delete). These operations allow users to manage data in the Data Grid. Refer to the following topic for more information: Implement CRUD Operations in a Data-Bound Grid.

View Example: How to Implement CRUD Operations in a Data-Bound Grid

Example

This example shows how to check if a user enters valid data into a row. Handle the GridViewBase.ValidateRow and GridViewBase.InvalidRowException events to validate the focused row’s data. If the data is invalid, do not allow the user to move focus to another row until the invalid values are corrected.

The Task class implements the IDataErrorInfo interface and allows you to get error descriptions for the entire row and individual cells (data source fields). Error icons appear in cells that contain invalid values. Hover the mouse pointer over an error icon to display a tooltip with an error description.

DevExpress WPF | Grid Control - Validate Data Rows

View Example: How to Validate Data Rows

<dxg:GridControl x:Name="grid" AutoGenerateColumns="AddNew">
    <dxg:GridControl.View>
        <dxg:TableView AutoWidth="True"
                       ValidateRow="OnValidateRow"
                       InvalidRowException="OnInvalidRowException"/>
    </dxg:GridControl.View>
</dxg:GridControl>
public class Task : IDataErrorInfo {
    public string TaskName { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }

    string IDataErrorInfo.Error => StartDate > EndDate ? "Either Start Date or End Date should be corrected." : null;
    string IDataErrorInfo.this[string columnName] {
        get {
            switch(columnName) {
                case nameof(StartDate):
                    if(StartDate > EndDate)
                        return "Start Date must be less than End Date";
                    break;
                case nameof(EndDate):
                    if(StartDate > EndDate)
                        return "End Date must be greater than Start Date";
                    break;
                case nameof(TaskName):
                    if(string.IsNullOrEmpty(TaskName))
                        return "Enter a task name";
                    break;
            }
            return null;
        }
    }
}
public partial class MainWindow : Window {
// ...
    void OnValidateRow(object sender, GridRowValidationEventArgs e) {
        var task = (Task)e.Row;
        if(task.StartDate > task.EndDate)
            e.SetError("Start Date must be less than End Date");
        if(string.IsNullOrEmpty(task.TaskName))
            e.SetError("Enter a task name");
    }

    void OnInvalidRowException(object sender, InvalidRowExceptionEventArgs e) {
        e.ExceptionMode = ExceptionMode.NoAction;
    }
}

The following code snippets (auto-collected from DevExpress Examples) contain references to the ValidateRow event.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

See Also