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

TextEditSettings

 TextEditSettings.MaskType = MaskType.Numeric;

Boolean (nullable Boolean) data type

CheckEditSettings

Decimal

TextEditSettings

 TextEditSettings.MaskType = MaskType.Simple;

 TextEditSettings.Mask = 'C';

 TextEditSettings.MaskPlaceHolder= '';

Enum data type

ComboBoxEditSettings

 LookUpEditSettingsBase.ItemsSource = new EnumItemsSource;

DateTime (nullable DateTime) data type

or

[DataType(DataType.Date)] attribute

DateEditSettings

Numeric data type

and

[DataType(DataType.PhoneNumber)] or [PhoneAttribute] attribute

TextEditSettings

 TextEditSettings.MaskType = MaskType.Simple;

 TextEditSettings.Mask = '(000) 000-0000';

 TextEditSettings.MaskUseAsDisplayFormat= true;

DateTime data type

and

[DataType(DataType.DateTime)] attribute

DateEditSettings

 TextEditSettings.Mask = 'g';

 TextEditSettings.MaskUseAsDisplayFormat= true;

DateTime data type

and

[DataType(DataType.Time)] attribute

DateEditSettings

 TextEditSettings.Mask = 't';

 TextEditSettings.MaskUseAsDisplayFormat= true;

String data type

and

[DataType(DataType.MultilineText)] attribute

MemoEditSettings

String data type

and

[DataType(DataType.Password)] attribute

PasswordBoxEditSettings

[Url] attribute or [DataType(DataType.Url)]

HyperlinkEditSettings

[DataType(DataType.ImageUrl)]

PopupImageEditSettings

[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.

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;
    }
}
See Also