GridViewBase.InvalidRowException Event

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

Namespace: DevExpress.Xpf.Grid

Assembly: DevExpress.Xpf.Grid.v20.1.dll

Declaration

public event InvalidRowExceptionEventHandler InvalidRowException
Public Event InvalidRowException As InvalidRowExceptionEventHandler

Event Data

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

Property Description
ErrorText Gets or sets the error description.
Exception Gets the exception that raised the GridViewBase.InvalidRowException event.
ExceptionMode Gets or sets a value that specifies how an exception should be handled.
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.
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. Inherited from RowEventArgs.
RowHandle Gets the processed row's handle. Inherited from RowEventArgs.
Source Gets or sets a reference to the object that raised the event. Inherited from RoutedEventArgs.
WindowCaption Gets or sets the error window's caption.

Remarks

Row validation is performed within the GridViewBase.ValidateRow event handler, when a row is about to lose focus. The InvalidRowException event allows you to override the default error presentation. This event occurs after the GridViewBase.ValidateRow event (provided that the focused row's validation has failed).

Use the event parameter's InvalidRowExceptionEventArgs.ExceptionMode property to specify the action performed in response to entering an invalid value. You can display a message box with an error description, suppress any action, throw an exception or discard new data and revert to an old value.

To learn more, see Row Validation.

Examples

This example shows how to check the validity of data entered by end users into a data row. The GridViewBase.ValidateRow and GridViewBase.InvalidRowException events are handled to validate the focused row's data, and if it is invalid, prevent the row focus from being moved to another row until invalid values are corrected.

Since the Task class does not support error notifications, it implements the IDXDataErrorInfo interface, providing two members to get error descriptions for the entire row and for individual cells (data source fields). This displays error icons within cells with invalid values. Pointing to such an error icon displays a tooltip with an error description.

NOTE

A complete sample project is available at https://github.com/DevExpress-Examples/how-to-validate-data-rows-e1593.

<Window x:Class="DXGrid_ValidateRow.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid" 
    Title="Window1" Height="300" Width="500">
    <Grid>
        <dxg:GridControl x:Name="grid">
            <dxg:GridControl.Columns>
                <dxg:GridColumn FieldName="TaskName" />
                <dxg:GridColumn FieldName="StartDate" />
                <dxg:GridColumn FieldName="EndDate" />
            </dxg:GridControl.Columns>
            <dxg:GridControl.View>
                <dxg:TableView AutoWidth="True" 
                               ValidateRow="TableView_ValidateRow" 
                               InvalidRowException="TableView_InvalidRowException" />
            </dxg:GridControl.View>
        </dxg:GridControl>
    </Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using DevExpress.Xpf.Grid;
using DevExpress.XtraEditors.DXErrorProvider;

namespace DXGrid_ValidateRow {
    public partial class Window1 : Window {
        public Window1() {
            InitializeComponent();
            grid.ItemsSource = TaskList.GetData();
        }

        private void TableView_ValidateRow(object sender, GridRowValidationEventArgs e) {
            DateTime startDate = ((Task)e.Row).StartDate;
            DateTime endDate = ((Task)e.Row).EndDate;
            e.IsValid = startDate < endDate;
        }

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

    public class TaskList {
        public static List<Task> GetData() {
            List<Task> data = new List<Task>();
            data.Add(new Task() { TaskName = "Complete Project A",
                StartDate = new DateTime(2009, 7, 17), EndDate = new DateTime(2009, 7, 10) });
            data.Add(new Task() { TaskName = "Test Website",
                StartDate = new DateTime(2009, 7, 10), EndDate = new DateTime(2009, 7, 12) });
            data.Add(new Task() { TaskName = "Publish Docs",
                StartDate = new DateTime(2009, 7, 4), EndDate = new DateTime(2009, 7, 6) });
            return data;
        }
    }

    public class Task : IDXDataErrorInfo {
        public string TaskName { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        void IDXDataErrorInfo.GetError(ErrorInfo info) {
            if (StartDate > EndDate) {
                SetErrorInfo(info,
                    "Either StartDate or EndDate should be corrected.",
                    ErrorType.Critical);
            }
        }
        void IDXDataErrorInfo.GetPropertyError(string propertyName, ErrorInfo info) {
            switch (propertyName) {
                case "StartDate":
                    if (StartDate > EndDate)
                        SetErrorInfo(info,
                            "StartDate must be less than EndDate",
                            ErrorType.Critical);
                    break;
                case "EndDate":
                    if (StartDate > EndDate)
                        SetErrorInfo(info,
                            "EndDate must be greater than StartDate",
                            ErrorType.Critical);
                    break;
                case "TaskName":
                    if (IsStringEmpty(TaskName))
                        SetErrorInfo(info,
                            "Task name hasn't been entered",
                            ErrorType.Information);
                    break;
            }
        }
        void SetErrorInfo(ErrorInfo info, string errorText, ErrorType errorType) {
            info.ErrorText = errorText;
            info.ErrorType = errorType;
        }
        bool IsStringEmpty(string str) {
            return (str != null && str.Trim().Length == 0);
        }
    }
}
See Also