Applying Data Annotations
- 2 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.v24.2 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
Set the DataControlBase.EnableSmartColumnsGeneration property to true to enable the Smart Columns Generation.
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’; |
DateOnly data type |
TextEditSettings.MaskType = MaskType.DateOnly; TextEditSettings.Mask = ‘d’; |
TimeOnly data type |
TextEditSettings.MaskType = MaskType.TimeOnly; 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.