New to Telerik UI for WinUI? Download free 30-day trial

Validation

RadDataGrid allows you to validate the data and display hints in case the validation is not passed. In order to benefit from this feature, the objects populating the ItemsSource of the control should implement the INotifyDataErrorInfo interface.

ValidateViewModelBase

You can use the ValidateViewModelBase class, which provides an implementation of INotifyDataErrorInfo in order to quickly setup your objects. Examples 1 and 2 demonstrate how to populate the RadDataGrid with objects that include some custom validation logic.

Example 1: Defining the RadDataGrid

<Grid xmlns:telerikGrid="using:Telerik.UI.Xaml.Controls.Grid"> 
    <Grid.DataContext> 
        <local:ViewModel /> 
    </Grid.DataContext> 
    <telerikGrid:RadDataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False" UserEditMode="Inline" > 
        <telerikGrid:RadDataGrid.Columns> 
            <telerikGrid:DataGridDateColumn PropertyName="Date" CellContentFormat="{}{0:d}"/> 
        </telerikGrid:RadDataGrid.Columns> 
    </telerikGrid:RadDataGrid> 
</Grid> 

Example 2: Model and ViewModel

public class ViewModel 
{ 
    public ViewModel() 
    { 
        this.Items = this.GetData(); 
    } 
 
    public ObservableCollection<Data> Items { get; set; } 
 
    private ObservableCollection<Data> GetData() 
    { 
        var data = new ObservableCollection<Data>(); 
        for (var i = 0; i < 10; i++) 
        { 
            data.Add(new Data { Date = DateTime.Today.AddDays(-i) }); 
        } 
 
        return data; 
    } 
} 
public class Data : ValidateViewModelBase 
{ 
    private readonly DateTimeOffset maxDate = DateTime.Today.AddDays(10); 
 
    private DateTimeOffset date; 
 
    public DateTimeOffset Date 
    { 
        get 
        { 
            return this.date; 
        } 
        set 
        { 
            this.date = value; 
 
            if (this.date > this.maxDate) 
            { 
                this.AddError("Date", string.Format("Date cannot be set after {0:d}.", this.maxDate)); 
            } 
            else 
            { 
                this.RemoveErrors("Date"); 
            } 
 
            this.OnPropertyChanged(); 
        } 
    } 
} 

Figure 1: Result from Examples 1 and 2

DataGrid Validation

See Also

In this article
Not finding the help you need?