This example shows how to apply DataAnnotations attributes that specify data validation rules. The RequiredAttribute is applied to the 'Name' and 'Department' data fields. The StringLengthAttribute is applied to the 'Position' data field.
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public class Employee {
public int ID { get; set; }
public int ParentID { get; set; }
[Required(AllowEmptyStrings = false, ErrorMessage = "The Name cannot be empty. Please correct.")]
public string Name { get; set; }
[StringLength(20)]
public string Position { get; set; }
[Required(AllowEmptyStrings = false, ErrorMessage = "The Department cannot be empty. Please correct.")]
public string Department { get; set; }
}
public static class Stuff {
public static List<Employee> GetStuff() {
List<Employee> stuff = new List<Employee>();
stuff.Add(new Employee() { ID = 1, ParentID = 0, Name = "Gregory S. Price", Department = "", Position = "President" });
stuff.Add(new Employee() { ID = 2, ParentID = 1, Name = string.Empty, Department = "Marketing", Position = "Vice President" });
stuff.Add(new Employee() { ID = 3, ParentID = 1, Name = "John C. Powell", Department = "Operations", Position = "Vice President" });
stuff.Add(new Employee() { ID = 4, ParentID = 1, Name = "Christian P. Laclair", Department = "Production", Position = "Vice President" });
stuff.Add(new Employee() { ID = 5, ParentID = 1, Name = string.Empty, Department = "Finance", Position = "Vice President" });
stuff.Add(new Employee() { ID = 6, ParentID = 2, Name = "Brian C. Cowling", Department = "Marketing", Position = "Manager" });
stuff.Add(new Employee() { ID = 7, ParentID = 2, Name = "Thomas C. Dawson", Department = "Marketing", Position = "Manager" });
stuff.Add(new Employee() { ID = 8, ParentID = 2, Name = "", Department = "Marketing", Position = "Manager" });
stuff.Add(new Employee() { ID = 9, ParentID = 2, Name = "", Department = "Marketing", Position = "Manager" });
stuff.Add(new Employee() { ID = 10, ParentID = 3, Name = "Harold S. Brandes", Department = "Operations", Position = "Manager" });
stuff.Add(new Employee() { ID = 11, ParentID = 3, Name = "Michael S. Blevins", Department = "Operations", Position = "Manager" });
stuff.Add(new Employee() { ID = 12, ParentID = 3, Name = "Jan K. Sisk", Department = "Operations", Position = "Manager" });
stuff.Add(new Employee() { ID = 13, ParentID = 3, Name = "Sidney L. Holder", Department = "Operations", Position = "Manager" });
stuff.Add(new Employee() { ID = 14, ParentID = 4, Name = "James L. Kelsey", Department = "Production", Position = "Manager" });
stuff.Add(new Employee() { ID = 15, ParentID = 4, Name = "Howard M. Carpenter", Department = "Production", Position = "Manager" });
stuff.Add(new Employee() { ID = 16, ParentID = 4, Name = "Jennifer T. Tapia", Department = "Production", Position = "Manager" });
stuff.Add(new Employee() { ID = 17, ParentID = 5, Name = "Judith P. Underhill", Department = "Finance", Position = "Manager" });
stuff.Add(new Employee() { ID = 18, ParentID = 5, Name = "Russell E. Belton", Department = "Finance", Position = "Manager" });
return stuff;
}
}
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;
namespace DXGrid_AttributesBasedValidation {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
grid.ItemsSource = Stuff.GetStuff();
}
}
}
<Window x:Class="DXGrid_AttributesBasedValidation.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid">
<Grid>
<dxg:GridControl Name="grid" >
<dxg:GridControl.Columns>
<dxg:GridColumn FieldName="Name" />
<dxg:GridColumn FieldName="Position" />
<dxg:GridColumn FieldName="Department" />
</dxg:GridControl.Columns>
<dxg:GridControl.View>
<dxg:TableView Name="tableView1" AutoWidth="True"
ShowValidationAttributeErrors="True"
AllowCommitOnValidationAttributeError="False"
/>
</dxg:GridControl.View>
</dxg:GridControl>
</Grid>
</Window>
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel.DataAnnotations
Public Class Employee
Public Property ID() As Integer
Public Property ParentID() As Integer
<Required(AllowEmptyStrings := False, ErrorMessage := "The Name cannot be empty. Please correct.")> _
Public Property Name() As String
<StringLength(20)> _
Public Property Position() As String
<Required(AllowEmptyStrings := False, ErrorMessage := "The Department cannot be empty. Please correct.")> _
Public Property Department() As String
End Class
Public NotInheritable Class Stuff
Private Sub New()
End Sub
Public Shared Function GetStuff() As List(Of Employee)
Dim stuff_Renamed As New List(Of Employee)()
stuff_Renamed.Add(New Employee() With {.ID = 1, .ParentID = 0, .Name = "Gregory S. Price", .Department = "", .Position = "President"})
stuff_Renamed.Add(New Employee() With {.ID = 2, .ParentID = 1, .Name = String.Empty, .Department = "Marketing", .Position = "Vice President"})
stuff_Renamed.Add(New Employee() With {.ID = 3, .ParentID = 1, .Name = "John C. Powell", .Department = "Operations", .Position = "Vice President"})
stuff_Renamed.Add(New Employee() With {.ID = 4, .ParentID = 1, .Name = "Christian P. Laclair", .Department = "Production", .Position = "Vice President"})
stuff_Renamed.Add(New Employee() With {.ID = 5, .ParentID = 1, .Name = String.Empty, .Department = "Finance", .Position = "Vice President"})
stuff_Renamed.Add(New Employee() With {.ID = 6, .ParentID = 2, .Name = "Brian C. Cowling", .Department = "Marketing", .Position = "Manager"})
stuff_Renamed.Add(New Employee() With {.ID = 7, .ParentID = 2, .Name = "Thomas C. Dawson", .Department = "Marketing", .Position = "Manager"})
stuff_Renamed.Add(New Employee() With {.ID = 8, .ParentID = 2, .Name = "", .Department = "Marketing", .Position = "Manager"})
stuff_Renamed.Add(New Employee() With {.ID = 9, .ParentID = 2, .Name = "", .Department = "Marketing", .Position = "Manager"})
stuff_Renamed.Add(New Employee() With {.ID = 10, .ParentID = 3, .Name = "Harold S. Brandes", .Department = "Operations", .Position = "Manager"})
stuff_Renamed.Add(New Employee() With {.ID = 11, .ParentID = 3, .Name = "Michael S. Blevins", .Department = "Operations", .Position = "Manager"})
stuff_Renamed.Add(New Employee() With {.ID = 12, .ParentID = 3, .Name = "Jan K. Sisk", .Department = "Operations", .Position = "Manager"})
stuff_Renamed.Add(New Employee() With {.ID = 13, .ParentID = 3, .Name = "Sidney L. Holder", .Department = "Operations", .Position = "Manager"})
stuff_Renamed.Add(New Employee() With {.ID = 14, .ParentID = 4, .Name = "James L. Kelsey", .Department = "Production", .Position = "Manager"})
stuff_Renamed.Add(New Employee() With {.ID = 15, .ParentID = 4, .Name = "Howard M. Carpenter", .Department = "Production", .Position = "Manager"})
stuff_Renamed.Add(New Employee() With {.ID = 16, .ParentID = 4, .Name = "Jennifer T. Tapia", .Department = "Production", .Position = "Manager"})
stuff_Renamed.Add(New Employee() With {.ID = 17, .ParentID = 5, .Name = "Judith P. Underhill", .Department = "Finance", .Position = "Manager"})
stuff_Renamed.Add(New Employee() With {.ID = 18, .ParentID = 5, .Name = "Russell E. Belton", .Department = "Finance", .Position = "Manager"})
Return stuff_Renamed
End Function
End Class
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
Namespace DXGrid_AttributesBasedValidation
''' <summary>
''' Interaction logic for MainWindow.xaml
''' </summary>
Partial Public Class MainWindow
Inherits Window
Public Sub New()
InitializeComponent()
grid.ItemsSource = Stuff.GetStuff()
End Sub
End Class
End Namespace