Back to Devexpress

GridViewBase.InvalidRowExceptionCommand Property

wpf-devexpress-dot-xpf-dot-grid-dot-gridviewbase-108cd892.md

latest6.3 KB
Original Source

GridViewBase.InvalidRowExceptionCommand Property

Gets or sets a command that is executed 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 ICommand<InvalidRowExceptionArgs> InvalidRowExceptionCommand { get; set; }
vb
Public Property InvalidRowExceptionCommand As ICommand(Of InvalidRowExceptionArgs)

Property Value

TypeDescription
ICommand<InvalidRowExceptionArgs>

A command that is executed when a row fails validation.

|

Remarks

Bind a command to the InvalidRowExceptionCommand property to maintain a clean MVVM pattern. The command works like an InvalidRowException event handler and allows you to specify an error presentation in a View Model.

Create a command and bind it to the GridViewBase.ValidateRowCommand property to validate entered values. After this command is processed, the GridControl executes a command bound to the InvalidRowExceptionCommand property and allows you to specify how to display validation errors.

Use the InvalidRowExceptionArgs.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.

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

Example

The following example verifies data and does not allow users to move focus to another row until invalid values are corrected:

View Example: How to Validate Data Rows

xaml
<dxg:GridControl ItemsSource="{Binding TaskList}" AutoGenerateColumns="AddNew">
    <dxg:GridControl.View>
        <dxg:TableView AutoWidth="True"
                       ValidateRowCommand="{Binding ValidateRowCommand}"
                       InvalidRowExceptionCommand="{Binding InvalidRowCommand}"/>
    </dxg:GridControl.View>
</dxg:GridControl>
cs
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("Start Date must be less than End Date");
            if(string.IsNullOrEmpty(task.TaskName))
                return new ValidationErrorInfo("Enter a task name");
            return null;
        }

        [Command]
        public void InvalidRow(InvalidRowExceptionArgs args) {
            args.ExceptionMode = ExceptionMode.NoAction;
        }
    }
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("Start Date must be less than End Date")
        If String.IsNullOrEmpty(task.TaskName) Then Return New ValidationErrorInfo("Enter a task name")
        Return Nothing
    End Function

    <Command>
    Public Sub InvalidRow(ByVal args As InvalidRowExceptionArgs)
        args.ExceptionMode = ExceptionMode.NoAction
    End Sub
End Class

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

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_MVVM/MainWindow.xaml#L20

xml
ValidateRowCommand="{Binding ValidateRowCommand}"
                   InvalidRowExceptionCommand="{Binding InvalidRowCommand}" />
</dxg:GridControl.View>

wpf-data-grid-validate-data-rows/CS/ValidateRow_MVVM/MainWindow.xaml#L17

xml
ValidateRowCommand="{Binding ValidateRowCommand}"
                   InvalidRowExceptionCommand="{Binding InvalidRowCommand}"/>
</dxg:GridControl.View>

See Also

InvalidNodeExceptionCommand

GridViewBase Class

GridViewBase Members

DevExpress.Xpf.Grid Namespace