Log In
Home
Support
Demos
Documentation
Blogs
Training
Webinars
[Expand]General Information
[Collapse]WinForms Controls
  Prerequisites
 [Expand]What's Installed
 [Expand]Build an Application
 [Expand]Controls and Libraries
 [Collapse]Common Features
  [Collapse]Data Binding Common Concepts
    How to: Bind GridControl to Database and Implement Master-Detail Mode at Design Time
    How to: Bind a Control to a Database at Runtime
    How to: Bind a Control to Data Created at Runtime
    How to: Bind a Control to Data in an XML File
    Traditional Data Binding Methods
    Data Source Configuration Wizard
    Data Binding Mechanism in ADO.NET
    Binding Controls to XML Data
    Binding Controls to Data Created at Runtime
    Add Unbound Data to a Data-Aware Control (XtraGrid, XtraPivotGrid)
    Binding to Excel Data Sources
    Binding to LINQ to SQL Classes
    Unbound Sources
    Binding to Entity Framework Core
    Binding to OData
    Binding to SQL Data
    Data Annotation Attributes
    How to: Add Virtual Rows Using the UnboundSource Component
    Obtaining Fields Available in Data Source
  [Expand]Data Source Wizard
  [Expand]Expressions
  [Expand]Behaviors
  [Expand]Application Appearance
  [Expand]Filtering UI Context
  [Expand]High DPI Support
  [Expand]Scaffolding Wizard
  [Expand]Formatting Values
   HTML Text Formatting
  [Expand]Menus
  [Expand]Tooltip Management
  [Expand]Saving and Restoring Layouts
   Clipboard - Copy and Paste Operations. Data Formatting
   Version Compatibility: Default Property Values
  Get More Help
 [Expand]API Reference
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Expand]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Document Server
[Expand]Reporting
[Expand]Report Server
[Expand]Dashboard
[Expand]eXpressApp Framework
[Expand]CodeRush
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation

Data Annotation Attributes

DevExpress data-aware controls (GridControl, TreeList, VGridControl, PropertyGridControl and Data Layout Control) support Data Annotation Attributes provided by Microsoft. You can define these attributes when creating your data source in code. The grids and treelists that uses this data as their data source will recognize the attributes and automatically tweak their columns - re-arrange them, change their captions, format cell data, apply filters or validation etc. This means that using Data Annotation Attributes allows you to completely disregard the control designer, since none of the columns require manual customization.

For the Data Layout Control, even more complex UI customization is supported - certain data attributes not only allow you to apply display formats and validation rules, but to group layout items into specific groups and tabbed containers. See the Data Annotation Attributes in Data Layout Control topic to learn more.

To see a complete Data Annotation Attributes list, see this MSDN article. The tables below list the most common and frequently used attributes supported by the Grid Control and TreeList Control.

Expanded Data Display Attributes

Attribute Parameter Description Code Sample Screenshot
Display
MSDN link
Name Specifies the column caption for the auto-generated column. This attribute has no effect on columns created at design-time. Column captions set by this attribute can be overridden by using the GridColumn.Caption property.
AutoGenerateField Specifies whether or not this column should be automatically generated by a GridControl.
-
AutoGenerateFilter Overrides the OptionsColumnFilter.AllowFilter property value for this column.
-
Order Specifies the display order for this column (see the GridColumn.VisibleIndex property).
Can be set to -1 to hide this column.
Description Assigns a tool-tip to this column. This attribute affects only auto-generated columns and can be overridden by using the GridColumn.ToolTip property.
Editable
MSDN link
Specifies whether this column can be edited (see the OptionsColumn.AllowEdit property).
-
ReadOnly
MSDN link
Gets or sets whether this column is in read only mode (OptionsColumn.ReadOnly).
-
DisplayFormat
MSDN link
DataFormatString Specifies the display format for this column's records.
ApplyFormatInEditMode Specifies whether the current display format for this cell should remain visible when this cell is being edited.

Expanded Data Type Attributes

Attribute Parameter Description Code Sample Screenshot
MetadataType
MSDN link
Allows you to derive data annotation attributes from another class.
-
DataType
MSDN link
Specifies the type of this column's data.
Based on this info the grid control, applies the specific formatting and chooses the appropriate editor for column cells.
EnumDataType
MSDN link
Allows you to replace numeric enumerator values with corresponding declarations.

Expanded Validation Attributes

Attribute Parameter Description Code Sample Screenshot
StringLength
MSDN link
Specifies the maximum and minimum number of characters for string records within this column.
Range
MSDN link
All numeric records of this column should lie in specific range, set by using this attribute.
Required
MSDN link
If used, restricts an end-user leaving a cell within this column if this cell is currently empty.
Compare
MSDN link
Compares the field with another property. Entering values that differ from the compared column's value is restricted.

Also refer to the Data Binding topic to learn more about using Fluent API to add new and modify existing columns in Entity Framework Code First models.

Expanded Example

Show Me

A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=T162611.

The solution in this example includes a DataSource file that has three classes that provide grid data - CompanyPublicInfo, CompanyPrivateInfo and Product. Properties for all three of them derive Data Annotation Attributes from the CompanyProductMetadata class by using the MetadataType attribute. End-users can use the editor at the top of the form to call one of the Get... methods that will populate the grid with sample data.
Important

To work with Data Annotation Attributes, you need to reference the System.ComponentModel.DataAnnotations library in your solution.

How would you rate this topic?​​​​​​​