Back to Devexpress

GridViewBase.InvalidRowException Event

wpf-devexpress-dot-xpf-dot-grid-dot-gridviewbase-4e052b93.md

latest11.8 KB
Original Source

GridViewBase.InvalidRowException Event

Occurs when a row fails validation or cannot be saved to a data source.

Namespace : DevExpress.Xpf.Grid

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

NuGet Package : DevExpress.Wpf.Grid.Core

Declaration

csharp
public event InvalidRowExceptionEventHandler InvalidRowException
vb
Public Event InvalidRowException As InvalidRowExceptionEventHandler

Event Data

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

PropertyDescription
ErrorTextGets or sets the error description.
ExceptionGets the exception that raised the GridViewBase.InvalidRowException event.
ExceptionModeGets or sets a value that specifies how an exception should be handled.
HandledGets 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.
OriginalSourceGets the original reporting source as determined by pure hit testing, before any possible Source adjustment by a parent class. Inherited from RoutedEventArgs.
RoutedEventGets or sets the RoutedEvent associated with this RoutedEventArgs instance. Inherited from RoutedEventArgs.
RowGets the processed row. Inherited from RowEventArgs.
RowHandleGets the processed row’s handle. Inherited from RowEventArgs.
SourceGets or sets a reference to the object that raised the event. Inherited from RoutedEventArgs.
WindowCaptionGets or sets the error window’s caption.

The event data class exposes the following methods:

MethodDescription
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.

Remarks

Use the GridViewBase.ValidateRow event to validate entered values. After this event is handled, the InvalidRowException event occurs and allows you to specify how to display validation errors.

Use the InvalidRowExceptionEventArgs.ExceptionMode property to specify which action to perform when a user enters an invalid value. You can display a message box with an error description, suppress an action, throw an exception, or ignore the validation result.

If you want to maintain a clean MVVM pattern and specify an error presentation in a View Model, create a command and bind it to the InvalidRowExceptionCommand property.

Refer to the following help 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

The following code snippets (auto-collected from DevExpress Examples) contain references to the InvalidRowException 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.

wpf-data-grid-initialize-new-item-row-with-default-values/CS/NewItemRow_CodeBehind/MainWindow.xaml#L16

xml
ValidateRow="OnValidateRow"
                   InvalidRowException="OnInvalidRowException" />
</dxg:GridControl.View>

wpf-data-grid-validate-data-rows/CS/ValidateRow_CodeBehind/MainWindow.xaml#L12

xml
ValidateRow="OnValidateRow"
                   InvalidRowException="OnInvalidRowException"/>
</dxg:GridControl.View>

See Also

ValidateRow

CommitEditing

InvalidNodeException

GridViewBase Class

GridViewBase Members

DevExpress.Xpf.Grid Namespace