Back to Devexpress

GridRowValidationEventArgs Class

wpf-devexpress-dot-xpf-dot-grid-2f03e8fc.md

latest7.1 KB
Original Source

GridRowValidationEventArgs Class

Populates the GridViewBase.ValidateRow event handler with data.

Namespace : DevExpress.Xpf.Grid

Assembly : DevExpress.Xpf.Grid.v25.2.Core.dll

NuGet Package : DevExpress.Wpf.Grid.Core

Declaration

csharp
public class GridRowValidationEventArgs :
    ValidationEventArgs,
    IDataRowEventArgs
vb
Public Class GridRowValidationEventArgs
    Inherits ValidationEventArgs
    Implements IDataRowEventArgs

GridRowValidationEventArgs is the data class for the following events:

Remarks

The GridControl validates changes made within the focused row when you try to move focus to another row. Handle the GridViewBase.ValidateRow event to process the row validate operation.

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.

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.

View Example: How to Validate Data Rows

xaml
<dxg:GridControl x:Name="grid" AutoGenerateColumns="AddNew">
    <dxg:GridControl.View>
        <dxg:TableView AutoWidth="True"
                       ValidateRow="OnValidateRow"
                       InvalidRowException="OnInvalidRowException"/>
    </dxg:GridControl.View>
</dxg:GridControl>
cs
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;
    }
}
vb
Public Class Task
    Implements IDataErrorInfo

    Public Property TaskName As String

    Public Property StartDate As Date

    Public Property EndDate As Date

    Private ReadOnly Property [Error] As String Implements IDataErrorInfo.[Error]
        Get
            Return If(StartDate > EndDate, "Either Start Date or End Date should be corrected.", Nothing)
        End Get
    End Property

    Private ReadOnly Property Item(ByVal columnName As String) As String Implements IDataErrorInfo.Item
        Get
            Select Case columnName
                Case NameOf(Task.StartDate)
                    If StartDate > EndDate Then Return "Start Date must be less than End Date"
                Case NameOf(Task.EndDate)
                    If StartDate > EndDate Then Return "End Date must be greater than Start Date"
                Case NameOf(Task.TaskName)
                    If String.IsNullOrEmpty(TaskName) Then Return "Enter a task name"
            End Select

            Return Nothing
        End Get
    End Property
End Class

Public Partial Class MainWindow
    Inherits Window
' ...
    Private Sub OnValidateRow(ByVal sender As Object, ByVal e As GridRowValidationEventArgs)
        Dim task = CType(e.Row, Task)
        If task.StartDate > task.EndDate Then e.SetError("Start Date must be less than End Date")
        If String.IsNullOrEmpty(task.TaskName) Then e.SetError("Enter a task name")
    End Sub

    Private Sub OnInvalidRowException(ByVal sender As Object, ByVal e As InvalidRowExceptionEventArgs)
        e.ExceptionMode = ExceptionMode.NoAction
    End Sub
End Class

Inheritance

Object EventArgs RoutedEventArgs ValidationEventArgs GridRowValidationEventArgs GridCellValidationEventArgs

TreeListNodeValidationEventArgs

TreeListCellValidationEventArgs

See Also

GridRowValidationEventArgs Members

DevExpress.Xpf.Grid Namespace