Skip to main content

Data Annotation Attributes

  • 6 minutes to read

DevExpress data-aware controls (GridControl, TreeList, VGridControl, PropertyGridControl, Data Layout Control, etc.) support Microsoft Data Annotation Attributes. You can apply these attributes when you create a data source in code.

When bound to such data sources, grid and tree list controls recognize attributes and automatically adjust column settings and layout (for example, set column captions, apply filters and data formats, apply validation rules, etc.).

Note

A repository item assigned to a column overrides attribute settings.

The following sections describe attributes most commonly used in GridControl and TreeListControl. To see the full list of Microsoft Data Annotation Attributes, visit the MSDN documentation.

Data Display Attributes

Display

The Display attribute specifies the column caption for auto generated columns.

This attribute does not effect columns created at design-time. The GridColumn.Caption property overrides the attribute value.

[Display(ShortName = "Company", Name = "Company Name")]
public string CompanyName { get; set; }

The following code snippet applies the Display attribute to specific enumeration values:

[EnumDataType(typeof(ProductCategory))]
public int Category { get; set; }
public enum ProductCategory {
    Beverages = 1,
    Fruit = 2,
    [Display(Name = "Dairy Products")]
    DairyProducts = 3,
    [Display(Name = "Grains Cereals")]
    GrainsCereals = 4
}

AutoGenerateField

The AutoGenerateField attribute specifies whether a control automatically creates a column for this field.

[Display(AutoGenerateField = false, Description = "This column isn't created")]
public string AdditionalInfo { get; set; }

AutoGenerateFilter

The AutoGenerateFilter attribute enables or disables automatic filter UI generation for this field. This attribute applies only to auto generated columns. For columns created manually, use the OptionsColumnFilter.AllowFilter property.

[Display(AutoGenerateFilter = false)]
public string Email { get; set; }

Order

The Order attribute specifies the column display order (see GridColumn.VisibleIndex).

[Display(Order = 2)]
public string Country { get; set; }

[Display(Order = 1)]
public string City { get; set; }

Tip

Set the Order attribute to -1 to hide the column.

Description

The Description attribute specifies a tooltip for the column.

The GridColumn.ToolTip property overrides the attribute value.

Grid Control - Data Annotation Attributes - Description

[Display(Description = "The amount of currently available product")]
public int Quantity { get; set; }

Editable

The Editable attribute specifies whether a user can edit column values (see OptionsColumn.AllowEdit).

[Editable(false)]
public string City { get; set; }

ReadOnly

The ReadOnly attribute sets the column to read only mode (OptionsColumn.ReadOnly).

[ReadOnly(true)]
public double UnitPrice { get; set; }

DisplayFormat

The DisplayFormat attribute specifies how the column displays formatted values.

DataFormatString

Specifies the display format for this column’s records.

WinForms Grid Control - Data Annotation Attributes - DataFormatString, DevExpress

[DisplayFormat(DataFormatString = "MMMM/yyyy")]
public object Date2 { get; set; }
ApplyFormatInEditMode

Specifies whether the display format for a cell should remain visible when the cell is being edited.

WinForms Grid Control - Data Annotation Attributes - FormatInEditMode, DevExpress

[DisplayFormat(DataFormatString = "p", ApplyFormatInEditMode = true)]
public object SalesVsTarget { get; set; }
ResourceType

Retrieves localized captions from a resource file.

[Display(ResourceType = typeof(Properties.Resources), Name = "User")]
public string User { get; private set; }

Data Type Attributes

MetadataType

The MetadataType attribute inherits data annotation attributes from another class.

[MetadataType(typeof(CompanyProductMetadata))]
public class Product {
    public double UnitPrice { get; set; }
}

public class CompanyProductMetadata {
    [ReadOnly(true)]
    public double UnitPrice { get; set; }
}

DataType

The DataType attribute specifies the data type.

The control selects the appropriate editor and formatting automatically.

WinForms Grid Control - Data Annotation Attributes - DataType, DevExpress

[DataType(DataType.PhoneNumber)]
public string Phone { get; set; }

EnumDataType

The EnumDataType attribute replaces numeric enum values with their text labels.

WinForms Grid Control - Data Annotation Attributes - EnumType, DevExpress

[EnumDataType(typeof(ProductCategory))]
public int Category { get; set; }

public enum ProductCategory {
    Beverages = 1,
    Fruit = 2,
    Vegetables = 3,
    Meat = 4,
    Condiments = 5,
    Confections = 6,
    DairyProducts = 7,
    GrainsCereals = 8,
    Seafood = 9
}

Validation Attributes

StringLength

The StringLength attribute specifies the minimum and maximum allowed length for string values.

WinForms Grid Control - Data Annotation Attributes - StringLength, DevExpress

[DataType(DataType.Password), StringLength(20, MinimumLength = 3)]
public string Password { get; set; }

Range

The Range attribute specifies a valid numeric range.

WinForms Grid Control - Data Annotation Attributes - Range, DevExpress

[DataType(DataType.Currency), Range(200, 5000)]
public int Currency { get; set; }

Required

The Required attribute marks the column as mandatory. Users cannot leave the cell empty.

WinForms Grid Control - Data Annotation Attributes - Required, DevExpress

[DataType(DataType.PhoneNumber), Required]
public string Phone { get; set; }

Compare

The Compare attribute compares the field value to another property. If values differ, the editor shows a validation error.

WinForms Grid Control - Data Annotation Attributes - Compare, DevExpress

[DataType(DataType.Password), Display(Name = "New Password")]
public string NewPassword { get; set; }

[DataType(DataType.Password), Compare("NewPassword",
     ErrorMessage="The new and confirmation passwords do not match"),
     Display(Name="Confirm Password")]
public string ConfirmPassword { get; set; }

See the following help topic for information about Fluent API in Entity Framework Code First models: Fluent API Support — Data Binding.

Data Layout Control Specifics

The Data Layout Control supports advanced customization.

You can use attributes to group layout items and create tab containers. See the following help topic for more information: Data Annotation Attributes in Data Layout Control.

Example

The following example defines a data source that uses attributes to describe columns, apply validation rules and formatting.

View Example: Data Source with Data Annotation Attributes