Interface-Based Validation
- 4 minutes to read
The GridControl
supports error notifications for custom data sources that implement standard .NET interfaces, such as IList
, ITypedList
, IListSource
, and IBindingList
. The grid automatically displays error icons in cells with invalid values. When a user hovers the mouse pointer over an error icon, the grid displays a tooltip with the error description.
Tip
Validate Grid Data on Load
Event and command-based validation methods are primarily designed to validate user input as it is entered. These methods are not intended for validating existing data when the grid loads.
To ensure that data is validated when the grid is initially displayed, use interface-based validation. This validation method allows the grid to automatically detect and display errors in the data source.
#Data Source Structure
#Data Source
The data source manages a collection of data records. This class should implement the IList
, IListSource
, ITypedList
, or IBindingList
interface:
grid.ItemsSource = new List<Task> {
new Task() {
TaskName = "Complete Project A",
StartDate = new DateTime(2009, 7, 17),
EndDate = new DateTime(2009, 7, 10)
},
new Task() {
TaskName = "Test Website",
StartDate = new DateTime(2009, 7, 10),
EndDate = new DateTime(2009, 7, 12)
}
};
#Data Record
A data record class should implement one of the following interfaces to support error notifications:
#IDXDataErrorInfo
The IDXDataErrorInfo interface offers enhanced validation and error indication. You can specify various error icons and types (for example, critical, warning) for better UI feedback.
The data record class should implement the following IDXDataErrorInfo
methods:
using DevExpress.XtraEditors.DXErrorProvider;
using System;
using System.Collections.ObjectModel;
public class TaskItem : IDXDataErrorInfo {
public string TaskName { get; set; }
public DateTime StartDate { get; set; } = DateTime.Today;
public DateTime EndDate { get; set; } = DateTime.Today.AddDays(1);
public void GetPropertyError(string propertyName, ErrorInfo info) {
switch (propertyName) {
case nameof(TaskName):
if (string.IsNullOrWhiteSpace(TaskName)) {
info.ErrorText = "The task name cannot be empty.";
info.ErrorType = ErrorType.Critical;
}
break;
case nameof(StartDate):
case nameof(EndDate):
if (StartDate > EndDate) {
info.ErrorText = "The start date must be before the end date.";
info.ErrorType = ErrorType.Warning;
}
break;
}
}
public void GetError(ErrorInfo info) {
if (string.IsNullOrWhiteSpace(TaskName)) {
info.ErrorText = "The task name is required.";
info.ErrorType = ErrorType.Critical;
}
else if (StartDate > EndDate) {
info.ErrorText = "Invalid date range.";
info.ErrorType = ErrorType.Warning;
}
}
}
#IDataErrorInfo
The IDataErrorInfo interface validates object properties and displays errors. The grid control displays error icons in cells containing invalid values.
The data record class should implement the following IDataErrorInfo
properties:
string Error { get; }
string this[string columnName] { get; }
public class Task : IDataErrorInfo {
public string TaskName { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
string IDataErrorInfo.Error => StartDate > EndDate ? "Invalid date range: the start date cannot be later than the end date." : null;
string IDataErrorInfo.this[string columnName] {
get {
switch(columnName) {
case nameof(StartDate):
if(StartDate > EndDate)
return "The start date must be before the end date.";
break;
case nameof(EndDate):
if(StartDate > EndDate)
return "The end date must be after the start date.";
break;
case nameof(TaskName):
if(string.IsNullOrEmpty(TaskName))
return "The task name cannot be empty.";
break;
}
return null;
}
}
}
#INotifyDataErrorInfo
The GridControl
detects and indicates errors from data sources that implement the INotifyDataErrorInfo interface.
To disable INotifyDataErrorInfo
validation, set the DataViewBase.ValidatesOnNotifyDataErrors property to false
.
Note
If the INotify
method returns multiple error messages, the grid displays the first error from the collection.
Tip
If the grid control is bound to a Data