Applying Data Annotations
- 6 minutes to read
Overview
The GridControl supports multiple Data Annotation Attributes that are used for customizing data classes, to specify how data is displayed from a data source, define validation rules, and set relationships between data classes.
To use data annotation attributes, reference the System.ComponentModel.DataAnnotations assembly. To use additional attributes for setting masks, reference the DevExpress.Mvvm.v20.1 assembly.
Note
Non-string IEnumerable properties are not automatically populated. To generate editors for such properties, use the [Display(AutoGenerateField = true)] data annotation attribute.
Smart Columns Generation
The Smart Columns Generation is enabled by default and can be disabled by setting the DataControlBase.EnableSmartColumnsGeneration property to false.
Below is a list of the Smart Columns Generation features.
Data Field | Generated Column’s Settings |
---|---|
Numeric (nullable numeric) data type | |
Boolean (nullable Boolean) data type | |
Decimal |
TextEditSettings.MaskType = MaskType.Simple; TextEditSettings.Mask = ‘C’; |
Enum data type |
LookUpEditSettingsBase.ItemsSource = new EnumItemsSource; |
DateTime (nullable DateTime) data type or [DataType(DataType.Date)] attribute | |
Numeric data type and [DataType(DataType.PhoneNumber)] or [PhoneAttribute] attribute |
TextEditSettings.MaskType = MaskType.Simple; TextEditSettings.Mask = ‘(000) 000-0000’; |
DateTime data type and [DataType(DataType.DateTime)] attribute |
TextEditSettings.Mask = ‘g’; |
DateTime data type and [DataType(DataType.Time)] attribute |
TextEditSettings.Mask = ‘t’; |
String data type and [DataType(DataType.MultilineText)] attribute | |
String data type and [DataType(DataType.Password)] attribute | |
[Url] attribute or [DataType(DataType.Url)] | |
[DataType(DataType.ImageUrl)] | |
[ReadOnly(true)] attribute | ColumnBase.ReadOnly = true; |
[Editable(false)] attribute | ColumnBase.AllowEditing = false; |
[Display(Order<0)] attribute or [HiddenAttribute] attribute | BaseColumn.Visible = false; |
[Display(Description=DESCRIPTION)] attribute | BaseColumn.HeaderToolTip = ‘DESCRIPTION’; |
[Display(ShortName=NAME)] attribute or [Display(Name=NAME)] attribute | BaseColumn.Header = ‘NAME’; |
[Display(GroupName = “GROUPNAME”)] attribute | The column is placed into the GridControlBand.Columns collection of the band which BaseColumn.Name property is set to ‘GROUPNAME’. |
In addition, the following attributes allow you to specify the editor’s mask settings: DateTimeMaskAttribute, NumericMaskAttribute, RegExMaskAttribute, RegularMaskAttribute, and SimpleMaskAttribute.
Example
This example shows how to apply data annotation attributes to 'Product' class members.
Note
A complete sample project is available at https://github.com/DevExpress-Examples/how-to-apply-data-annotations-e2579.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
public class Product {
[Display(Order = 0, ShortName = "Company")]
public string CompanyName { get; set; }
public string Country { get; set; }
public string City { get; set; }
[ReadOnly(true)]
public double UnitPrice { get; set; }
[Display(Description = "This field is hidden", Order = -1)]
public int Quantity { get; set; }
[Display(AutoGenerateField = false, Description = "This column isn't created")]
public string AdditionalInfo { get; set; }
}
public class ProductList {
static public List<Product> GetData() {
List<Product> list = new List<Product>();
list.Add(new Product() { CompanyName = "Island Trading", Country = "UK", City = "Cowes", UnitPrice = 19, Quantity = 10 });
list.Add(new Product() { CompanyName = "Reggiani Caseifici", Country = "Italy", City = "Reggio Emilia", UnitPrice = 12.5, Quantity = 16 });
list.Add(new Product() { CompanyName = "Ricardo Adocicados", Country = "Brazil", City = "Rio de Janeiro", UnitPrice = 19, Quantity = 12 });
list.Add(new Product() { CompanyName = "QUICK-Stop", Country = "Germany", City = "QUICK-Stop", UnitPrice = 22, Quantity = 50 });
list.Add(new Product() { CompanyName = "Split Rail Beer & Ale", Country = "USA", City = "Reggio Emilia", UnitPrice = 18, Quantity = 20 });
list.Add(new Product() { CompanyName = "Ernst Handel", Country = "Austria", City = "Graz", UnitPrice = 21, Quantity = 52 });
list.Add(new Product() { CompanyName = "Save-a-lot Markets", Country = "USA", City = "Boise", UnitPrice = 7.75, Quantity = 120 });
list.Add(new Product() { CompanyName = "Tortuga Restaurante", Country = "Mexico", City = "México D.F.", UnitPrice = 21, Quantity = 15 });
list.Add(new Product() { CompanyName = "Bottom-Dollar Markets", Country = "Canada", City = "Tsawwassen", UnitPrice = 44, Quantity = 16 });
return list;
}
}