Data Annotation Attributes
- 13 minutes to read
DevExpress data-aware controls (GridControl, TreeList, VGridControl, PropertyGridControl and Data Layout Control) support Microsoft Data Annotation Attributes. You can apply data annotation attributes when creating a data source in code. Grid and treelist controls bound to such data sources recognize the attributes and automatically tweak columns - re-arrange columns, change header captions, apply filters, format and validate cell values, etc.
Note
A repository item assigned to a column overrides settings specified by Data Annotation Attributes.
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.
Data Display Attributes
Attribute | Parameter (if any) | Description |
Code Sample | ||
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. | |
Example 1
Example 2 (Enum Properties)
| ||
AutoGenerateField | Specifies whether a control automatically generates a column for this field. | |
AutoGenerateFilter | Specifies whether the filter UI is automatically generated for this field. Applies to automatically generated columns only. If a column is created manually, use the OptionsColumnFilter.AllowFilter property to generate the filter UI for that 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. The GridColumn.ToolTip property overrides this attribute description. | |
Specifies whether this column can be edited (see the OptionsColumn.AllowEdit property). | ||
Gets or sets whether this column is in read only mode (OptionsColumn.ReadOnly). | ||
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. | |
Allows you to specify a value from a resx file to show localized captions. |
Data Type Attributes
Attribute | Parameter (if any) | Description |
Code Sample | ||
Allows you to derive data annotation attributes from another class. | ||
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. | ||
Allows you to replace numeric enumerator values with corresponding declarations. | ||
Validation Attributes
Attribute | Parameter (if any) | Description |
Code Sample | ||
Specifies the maximum and minimum number of characters for string records within this column. | ||
All numeric records of this column should lie in specific range, set by using this attribute. | ||
If used, restricts an end-user leaving a cell within this column if this cell is currently empty. | ||
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.
Example
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.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GridDataAttributes {
[MetadataType(typeof(CompanyProductMetadata))]
public class CompanyPublicInfo {
public string CompanyName { get; set; }
public string Country { get; set; }
public string City { get; set; }
public string Url { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string AdditionalInfo { get; set; }
}
[MetadataType(typeof(CompanyProductMetadata))]
public class CompanyPrivateInfo {
public string Password { get; set; }
public DateTime Date2 { get; set; }
public double Sales { get; set; }
public double Profit { get; set; }
public double SalesVsTarget { get; set; }
public double MarketShare { get; set; }
public double CustomersSatisfaction { get; set; }
}
public class Product {
[ReadOnly(true)]
public double UnitPrice { get; set; }
[EnumDataType(typeof(ProductCategory))]
public int Category { get; set; }
[Display(Description = "The amount of currently available product")]
public int Quantity { get; set; }
[DataType(DataType.Text), Display(Order = -1)]
public string Text { get; set; }
[DataType(DataType.MultilineText)]
public string MultilineText { get; set; }
[DataType(DataType.Currency), Range(200, 5000)]
public int Currency { get; set; }
[DataType(DataType.Date)]
public DateTime Date { get; set; }
[DataType(DataType.Time)]
public DateTime Time { get; set; }
}
public class CompanyProductMetadata {
[Display(ShortName = "Company", Name = "Company Name", AutoGenerateFilter = false)]
public object CompanyName;
[Display(Order = 2)]
public object Country;
[Display(Order = 1), Editable(false)]
public object City;
[DataType(DataType.Url)]
public object Url;
[DataType(DataType.EmailAddress)]
public object Email;
[DataType(DataType.PhoneNumber), Required]
public object Phone;
[DataType(DataType.Text), Display(Order = -1)]
public object Text;
[Display(AutoGenerateField = false, Description = "This column isn't created")]
public object AdditionalInfo;
[DataType(DataType.Password), StringLength(20, MinimumLength = 3)]
public object Password;
[DisplayFormat(DataFormatString = "MMMM/yyyy"), Display(Name = "Date 2")]
public object Date2;
[DisplayFormat(DataFormatString = "#,##0,,M")]
public object Sales;
[DisplayFormat(DataFormatString = "#,##0,,M")]
public object Profit;
[DisplayFormat(DataFormatString = "p", ApplyFormatInEditMode = true), Display(Name = "Sales vs Target")]
public object SalesVsTarget;
[DisplayFormat(DataFormatString = "p0", ApplyFormatInEditMode = false)]
public object MarketShare;
[Display(Name = "Cust Satisfaction")]
public object CustomersSatisfaction;
}
public enum ProductCategory {
Beverages = 1,
Fruit = 2,
Vegetables = 3,
Meat = 4,
Condiments = 5,
Confections = 6,
DairyProducts = 7,
GrainsCereals = 8,
Seafood = 9
}
public class GridSampleDataList {
static public List<CompanyPrivateInfo> GetCompanyPrivateInfo() {
return new List<CompanyPrivateInfo> {
new CompanyPrivateInfo() {
CustomersSatisfaction = 3.1,
Date2 = DateTime.Now,
MarketShare = 42,
Password = "123qwerty",
Profit = 4951515,
Sales = 311414134,
SalesVsTarget = 0.0277,
}
};
}
static public List<CompanyPublicInfo> GetCompanyPublicInfo() {
return new List<CompanyPublicInfo> {
new CompanyPublicInfo() {
AdditionalInfo = "Some Info",
City = "Glendale",
CompanyName = "Developer Express",
Country = "USA",
Email = "info@devexpress.com",
Phone = "1234567890",
Url = "www.devexpress.com",
}
};
}
static public List<Product> GetProductSample() {
return new List<Product> {
new Product() {
Currency = 1000,
Category = 2,
Date = DateTime.Now,
MultilineText = "Line1\r\nLine2\r\nLine3",
Quantity = 321,
Text = "Sample Text",
Time = DateTime.Now,
UnitPrice = 1800,
}
};
}
}
}