Skip to main content

Validating Data

The following page provides a list of examples that demonstrate how to validate data.

note

All examples in this topic assume that the grid is bound to the Orders table of the Northwind database, unless stated otherwise.

Providing a cell error style

The following example demonstrates how to provide a new style that will change the foreground color of a cell when its value fails the validation process.

<Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"
xmlns:local="clr-namespace:Xceed.Wpf.Documentation">
<Grid.Resources>
<xcdg:DataGridCollectionViewSource x:Key="cvs_composers"
Source="{Binding Source={x:Static Application.Current},
Path=Composers}"/>
<Style x:Key="cell_error" TargetType="{x:Type xcdg:DataCell}">
<Setter Property="Foreground" Value="Red"/>
</Style>
</Grid.Resources>
<xcdg:DataGridControl ItemsSource="{Binding Source={StaticResource cvs_composers}}"
CellErrorStyle="{StaticResource cell_error}">
<xcdg:DataGridControl.Columns>
<xcdg:Column FieldName="Period"
CellEditor="{StaticResource periodEditor}">
<xcdg:Column.CellValidationRules>
<local:PeriodVSCompositionCountCellValidationRule/>
</xcdg:Column.CellValidationRules>
</xcdg:Column>
<xcdg:Column FieldName="CompositionCount">
<xcdg:Column.CellValidationRules>
<local:PeriodVSCompositionCountCellValidationRule />
</xcdg:Column.CellValidationRules>
</xcdg:Column>
</xcdg:DataGridControl.Columns>
</xcdg:DataGridControl>
</Grid>

Providing binding-level validation

The following example demonstrates how to create a custom ValidationRule and apply it to a column's binding to provide binding-level validation.

The implementation for the YearValidationRule is provided below.

  <Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"
xmlns:local="clr-namespace:Xceed.Wpf.Documentation">
<Grid.Resources>
<xcdg:DataGridCollectionViewSource x:Key="cvs_composers"
Source="{Binding Source={x:Static Application.Current},
Path=Composers}"/>
</Grid.Resources>

<xcdg:DataGridControl ItemsSource="{Binding Source={StaticResource cvs_composers}}"
UpdateSourceTrigger="RowEndingEdit">
<xcdg:DataGridControl.Columns>
<xcdg:Column FieldName="BirthYear">
<xcdg:Column.DisplayMemberBindingInfo>
<xcdg:DataGridBindingInfo Path="BirthYear">
<xcdg:DataGridBindingInfo.ValidationRules>
<local:YearValidationRule />
</xcdg:DataGridBindingInfo.ValidationRules>
</xcdg:DataGridBindingInfo>
</xcdg:Column.DisplayMemberBindingInfo>
</xcdg:Column>
<xcdg:Column FieldName="DeathYear">
<xcdg:Column.DisplayMemberBindingInfo>
<xcdg:DataGridBindingInfo Path="DeathYear">
<xcdg:DataGridBindingInfo.ValidationRules>
<local:YearValidationRule />
</xcdg:DataGridBindingInfo.ValidationRules>
</xcdg:DataGridBindingInfo>
</xcdg:Column.DisplayMemberBindingInfo>
</xcdg:Column>
</xcdg:DataGridControl.Columns>
</xcdg:DataGridControl>
</Grid>

Providing UI-level validation

The following example demonstrates how to create a custom CellValidationRule and add it to a column's CellValidationRules collection to provide UI-level validation.

The implementation for the PeriodVSCompositionCountCellValidationRule and Person class are provided below.

<Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"
xmlns:local="clr-namespace:Xceed.Wpf.Documentation">
<Grid.Resources>
<xcdg:DataGridCollectionViewSource x:Key="cvs_composers"
Source="{Binding Source={x:Static Application.Current},
Path=Composers}"/>
<!--A data provider to bind to the Period enum-->
<ObjectDataProvider x:Key="periods"
MethodName="GetValues"
ObjectType="{x:Type local:Period}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="local:Period"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<!--A cell editor that will be used to edit a Period column with a combo box-->
<xcdg:CellEditor x:Key="periodEditor">
<xcdg:CellEditor.EditTemplate>
<DataTemplate>
<ComboBox BorderThickness="0"
MinHeight="22"
VerticalContentAlignment="Top"
SelectedValuePath="."
ItemsSource="{Binding Source={StaticResource periods}}"
SelectedValue="{xcdg:CellEditorBinding}">
<ComboBox.Resources>
<Style TargetType="Popup">
<Setter Property="TextElement.Foreground"
Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}" />
</Style>
</ComboBox.Resources>
</ComboBox>
</DataTemplate>
</xcdg:CellEditor.EditTemplate>
</xcdg:CellEditor>
</Grid.Resources>
<xcdg:DataGridControl ItemsSource="{Binding Source={StaticResource cvs_composers}}"
UpdateSourceTrigger="RowEndingEdit">
<xcdg:DataGridControl.Columns>
<xcdg:Column FieldName="Period"
CellEditor="{StaticResource periodEditor}">
<xcdg:Column.CellValidationRules>
<local:PeriodVSCompositionCountCellValidationRule/>
</xcdg:Column.CellValidationRules>
</xcdg:Column>
<xcdg:Column FieldName="CompositionCount">
<xcdg:Column.CellValidationRules>
<local:PeriodVSCompositionCountCellValidationRule />
</xcdg:Column.CellValidationRules>
</xcdg:Column>
</xcdg:DataGridControl.Columns>
</xcdg:DataGridControl>
</Grid>

Implementation of the Person class

  using System.ComponentModel;
using System.Windows.Media;
namespace Xceed.Wpf.Documentation
{
public class Person: INotifyPropertyChanged, IDataErrorInfo
{
public Person( int personID, string firstName, string lastName, int age )
{
m_personID = personID;
m_firstName = firstName;
m_lastName = lastName;
m_age = age;
}
public Person()
{
}
public int PersonID
{
get
{
return m_personID;
}
}
public string FirstName
{
get
{
return m_firstName;
}
set
{
if( m_firstName != value )
{
m_firstName = value;
this.OnPropertyChanged( "FirstName" );
}
}
}
public string LastName
{
get
{
return m_lastName;
}
set
{
if( m_lastName != value )
{
m_lastName = value;
this.OnPropertyChanged( "LastName" );
}
}
}
public int Age
{
get
{
return m_age;
}
set
{
if( m_age == value )
return;
m_age = value;
this.OnPropertyChanged( "Age" );
}
}
// INotifyPropertyChanged implementation
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged( string propertyName )
{
if( this.PropertyChanged != null )
this.PropertyChanged( this, new PropertyChangedEventArgs( propertyName ) );
}
// IDataErrorInfo implementation
[EditorBrowsable( EditorBrowsableState.Never ) ]
[Browsable( false )]
public string Error
{
get
{
return "IDataErrorInfo Error Message";
}
}
[EditorBrowsable( EditorBrowsableState.Never )]
[Browsable( false )]
public string this[ string propertyName ]
{
get
{
if( propertyName == "Age" )
{
if( m_age < 18 )
return "An employee must be 18 years or older.";
}
return string.Empty;
}
}
private string m_firstName = string.Empty;
private string m_lastName = string.Empty;
private int m_personID;
private int m_age = 18;
}
}