InvalidRowExceptionEventArgs Class
Namespace: DevExpress.Xpf.Grid
Assembly: DevExpress.Xpf.Grid.v19.1.dll
Declaration
public class InvalidRowExceptionEventArgs :
RowEventArgs,
IInvalidRowExceptionEventArgs
Public Class InvalidRowExceptionEventArgs
Inherits RowEventArgs
Implements IInvalidRowExceptionEventArgs
The GridViewBase.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).
The event parameter’s InvalidRowExceptionEventArgs.ExceptionMode property allows you 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. The error description can be specified using the InvalidRowExceptionEventArgs.ErrorText property.
An exception that raised this event is returned by the InvalidRowExceptionEventArgs.Exception property.
To learn more, see Row Validation.
Example
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 doesn’t 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.
<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>
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Windows.Navigation
Imports System.Windows.Shapes
Imports System.Data
Imports DevExpress.XtraEditors.DXErrorProvider
Namespace DXGrid_ValidateRow
Partial Public Class Window1
Inherits Window
Public Sub New()
InitializeComponent()
grid.ItemsSource = TaskList.GetData()
End Sub
Private Sub TableView_ValidateRow(ByVal sender As Object, _
ByVal e As DevExpress.Xpf.Grid.GridRowValidationEventArgs)
Dim startDate As DateTime = (CType(e.Row, Task)).StartDate
Dim endDate As DateTime = (CType(e.Row, Task)).EndDate
e.IsValid = startDate < endDate
End Sub
Private Sub TableView_InvalidRowException(ByVal sender As Object, _
ByVal e As DevExpress.Xpf.Grid.InvalidRowExceptionEventArgs)
e.ExceptionMode = DevExpress.Xpf.Grid.ExceptionMode.NoAction
End Sub
End Class
Public Class TaskList
Public Shared Function GetData() As List(Of Task)
Dim data As New List(Of Task)()
data.Add(New Task() With {.TaskName = "Complete Project A", _
.StartDate = New DateTime(2009, 7, 17), .EndDate = New DateTime(2009, 7, 10)})
data.Add(New Task() With {.TaskName = "Test Website", _
.StartDate = New DateTime(2009, 7, 10), .EndDate = New DateTime(2009, 7, 12)})
data.Add(New Task() With {.TaskName = "Publish Docs", _
.StartDate = New DateTime(2009, 7, 4), .EndDate = New DateTime(2009, 7, 6)})
Return data
End Function
End Class
Public Class Task
Implements IDXDataErrorInfo
Private privateTaskName As String
Public Property TaskName() As String
Get
Return privateTaskName
End Get
Set(ByVal value As String)
privateTaskName = value
End Set
End Property
Private privateStartDate As DateTime
Public Property StartDate() As DateTime
Get
Return privateStartDate
End Get
Set(ByVal value As DateTime)
privateStartDate = value
End Set
End Property
Private privateEndDate As DateTime
Public Property EndDate() As DateTime
Get
Return privateEndDate
End Get
Set(ByVal value As DateTime)
privateEndDate = value
End Set
End Property
Private Sub GetError(ByVal info As ErrorInfo) Implements IDXDataErrorInfo.GetError
If StartDate > EndDate Then
SetErrorInfo(info, _
"Either StartDate or EndDate should be corrected.", _
ErrorType.Critical)
End If
End Sub
Private Sub GetPropertyError(ByVal propertyName As String, _
ByVal info As ErrorInfo) Implements IDXDataErrorInfo.GetPropertyError
Select Case propertyName
Case "StartDate"
If StartDate > EndDate Then
SetErrorInfo(info, _
"StartDate must be less than EndDate", _
ErrorType.Critical)
End If
Case "EndDate"
If StartDate > EndDate Then
SetErrorInfo(info, _
"EndDate must be greater than StartDate", _
ErrorType.Critical)
End If
Case "TaskName"
If IsStringEmpty(TaskName) Then
SetErrorInfo(info, _
"Task name hasn't been entered", _
ErrorType.Information)
End If
End Select
End Sub
Private Sub SetErrorInfo(ByVal info As ErrorInfo, _
ByVal errorText As String, _
ByVal errorType As ErrorType)
info.ErrorText = errorText
info.ErrorType = errorType
End Sub
Private Function IsStringEmpty(ByVal str As String) As Boolean
Return (str IsNot Nothing AndAlso str.Trim().Length = 0)
End Function
End Class
End Namespace
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Configuration
Imports System.Data
Imports System.Linq
Imports System.Windows
Namespace DXGrid_ValidateRow
''' <summary>
''' Interaction logic for App.xaml
''' </summary>
Partial Public Class App
Inherits Application
End Class
End Namespace
<Application x:Class="DXGrid_ValidateRow.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="Window1.xaml">
<Application.Resources>
</Application.Resources>
</Application>
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