Export (0) Print
Expand All
Expand Minimize
This topic has not yet been rated - Rate this topic

DataGrid.RowValidationRules Property

Gets the rules that are used to validate the data in each row.

Namespace:  System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)
public ObservableCollection<ValidationRule> RowValidationRules { get; }

Property Value

Type: System.Collections.ObjectModel.ObservableCollection<ValidationRule>
The rules that are used to validate the data in each row.

The DataGrid control enables you to perform validation at both the cell and row level. With cell-level validation, you validate individual properties of a bound data object when a user updates a value. With row-level validation, you validate entire data objects when a user commits changes to a row. To create a custom validation rule, create a class that derives from the ValidationRule class and implement the Validate method. Add the custom validation rule to the RowValidationRules collection.

If the ItemBindingGroup property is used, the RowValidationRules property is ignored.

The following example demonstrates a ValidationRule that checks whether the StartDate property value for a Course object is earlier than its EndDate property value. This code example is part of a larger example provided for the How to: Implement Validation with the DataGrid Control topic.

public class CourseValidationRule : ValidationRule
{
    public override ValidationResult Validate(object value,
        System.Globalization.CultureInfo cultureInfo)
    {
        Course course = (value as BindingGroup).Items[0] as Course;
        if (course.StartDate > course.EndDate)
        {
            return new ValidationResult(false,
                "Start Date must be earlier than End Date.");
        }
        else
        {
            return ValidationResult.ValidResult;
        }
    }
}

The following example sets the RowValidationRules property in XAML. The ValidationStep property is set to UpdatedValue so that the validation occurs only after the bound data object is updated. When a user specifies an end date that is earlier than the start date, a red exclamation mark (!) appears in the row header.

<DataGrid.RowValidationRules>
  <local:CourseValidationRule ValidationStep="UpdatedValue"/>
</DataGrid.RowValidationRules>

.NET Framework

Supported in: 4.5.1, 4.5, 4

.NET Framework Client Profile

Supported in: 4

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.