GridCellValidationEventArgs Class
Namespace: DevExpress.Xpf.Grid
Assembly:
DevExpress.Xpf.Grid.v20.1.dll
NuGet Packages:
DevExpress.WindowsDesktop.Wpf.Grid.Core, DevExpress.Wpf.Grid.Core
Declaration
public class GridCellValidationEventArgs :
GridRowValidationEventArgs,
IDataCellEventArgs
Public Class GridCellValidationEventArgs
Inherits GridRowValidationEventArgs
Implements IDataCellEventArgs
To learn more, see Cell Validation.
Example
This example shows how to validate the focused cell's value. In this example, the product's price can be reduced by 30% if the product is discontinued.
<Window x:Class="DXGrid_ValidatingEditors.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
Title="Window1" Height="300" Width="600">
<Grid>
<dxg:GridControl x:Name="grid" ItemsSource="{Binding ProductList}">
<dxg:GridControl.Columns>
<dxg:GridColumn FieldName="ProductName">
<dxg:GridColumn.EditSettings>
<dxe:TextEditSettings AllowNullInput="False" />
</dxg:GridColumn.EditSettings>
</dxg:GridColumn>
<dxg:GridColumn FieldName="UnitPrice" Validate="GridColumn_Validate">
<dxg:GridColumn.EditSettings>
<dxe:SpinEditSettings DisplayFormat="c2" />
</dxg:GridColumn.EditSettings>
</dxg:GridColumn>
<dxg:GridColumn FieldName="Discontinued" />
</dxg:GridControl.Columns>
<dxg:GridControl.View>
<dxg:TableView AutoWidth="True" HiddenEditor="TableView_HiddenEditor" />
</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 System.Collections.ObjectModel;
namespace DXGrid_ValidatingEditors {
public partial class Window1 : Window {
public Window1() {
InitializeComponent();
this.DataContext = new MyViewModel();
}
private void GridColumn_Validate(object sender, GridCellValidationEventArgs e) {
bool discontinued = ((Product)e.Row).Discontinued;
if (discontinued) {
double discount = 100 - (Convert.ToDouble(e.Value) * 100) /
Convert.ToDouble(e.CellValue);
if (!(discount > 0 && discount <= 30)) {
e.IsValid = false;
e.ErrorType = DevExpress.XtraEditors.DXErrorProvider.ErrorType.Critical;
if (discount < 0) {
e.ErrorContent = string.Format("The price cannot be greater than ${0}",
Convert.ToDouble(e.CellValue));
return;
}
e.ErrorContent = string.Format(
"The discount cannot be greater than 30% (${0}). Please correct the price.",
Convert.ToDouble(e.CellValue)*0.7);
}
}
}
private void TableView_HiddenEditor(object sender, EditorEventArgs e) {
if (e.Column.FieldName != "Discontinued") return;
grid.View.CommitEditing();
}
}
public class MyViewModel {
public MyViewModel() {
CreateList();
}
public ObservableCollection<Product> ProductList { get; set; }
void CreateList() {
ProductList = new ObservableCollection<Product>();
Random r = new Random();
for (int i = 0; i < 20; i++) {
Product p = new Product(i);
p.UnitPrice = r.Next(1, 50);
ProductList.Add(p);
}
}
}
public class Product {
public Product(int i) {
ProductName = "Product" + i;
Discontinued = i % 5 == 0;
}
public string ProductName { get; set; }
public int UnitPrice { get; set; }
public bool Discontinued { get; set; }
}
}
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.Xpf.Grid
Imports System.Collections.ObjectModel
Namespace DXGrid_ValidatingEditors
Partial Public Class Window1
Inherits Window
Public Sub New()
InitializeComponent()
Me.DataContext = New MyViewModel()
End Sub
Private Sub GridColumn_Validate(ByVal sender As Object, ByVal e As GridCellValidationEventArgs)
Dim discontinued As Boolean = CType(e.Row, Product).Discontinued
If discontinued Then
Dim discount As Double = 100 - (Convert.ToDouble(e.Value) * 100) / Convert.ToDouble(e.CellValue)
If Not(discount > 0 AndAlso discount <= 30) Then
e.IsValid = False
e.ErrorType = DevExpress.XtraEditors.DXErrorProvider.ErrorType.Critical
If discount < 0 Then
e.ErrorContent = String.Format("The price cannot be greater than ${0}", Convert.ToDouble(e.CellValue))
Return
End If
e.ErrorContent = String.Format("The discount cannot be greater than 30% (${0}). Please correct the price.", Convert.ToDouble(e.CellValue)*0.7)
End If
End If
End Sub
Private Sub TableView_HiddenEditor(ByVal sender As Object, ByVal e As EditorEventArgs)
If e.Column.FieldName <> "Discontinued" Then
Return
End If
grid.View.CommitEditing()
End Sub
End Class
Public Class MyViewModel
Public Sub New()
CreateList()
End Sub
Public Property ProductList() As ObservableCollection(Of Product)
Private Sub CreateList()
ProductList = New ObservableCollection(Of Product)()
Dim r As New Random()
For i As Integer = 0 To 19
Dim p As New Product(i)
p.UnitPrice = r.Next(1, 50)
ProductList.Add(p)
Next i
End Sub
End Class
Public Class Product
Public Sub New(ByVal i As Integer)
ProductName = "Product" & i
Discontinued = i Mod 5 = 0
End Sub
Public Property ProductName() As String
Public Property UnitPrice() As Integer
Public Property Discontinued() As Boolean
End Class
End Namespace
See Also