[Expand]General Information
[Expand]WinForms Controls
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Collapse]WPF Controls
  Prerequisites
 [Expand]What's Installed
 [Expand]Common Concepts
 [Expand]MVVM Framework
 [Collapse]Controls and Libraries
   Reporting
  [Collapse]Data Grid
   [Expand]Getting Started
   [Expand]Implementation Details
   [Expand]Views
   [Expand]Grid View Data Layout
   [Collapse]Binding to Data
     Binding to Data Overview
     Binding to ICollectionView
    [Expand]Server Mode
    [Expand]Binding to any Data Source with Virtual Sources
    [Expand]TreeListView Data Binding
     Applying Data Annotations
     Unbound Columns
     Posting Data to a Connected Database
     Managing Multi-Thread Data Updates
   [Expand]Master-Detail Data Representation
   [Expand]Data Editing and Validation
   [Expand]Grouping
   [Expand]Selection
   [Expand]Filtering and Searching
   [Expand]Sorting
   [Expand]Data Summaries
   [Expand]Paging and Scrolling
   [Expand]Focus and Navigation
   [Expand]Conditional Formatting
   [Expand]Drag-and-Drop
   [Expand]Appearance Customization
   [Expand]MVVM Enhancements
   [Expand]Printing and Exporting
   [Expand]End-User Interaction
   [Expand]Miscellaneous
   [Expand]Performance Improvement
   [Expand]Design-Time Features
   [Expand]Visual Elements
   [Expand]End-User Capabilities
   [Expand]Examples
  [Expand]Ribbon, Bars and Menu
  [Expand]Charts Suite
  [Expand]Pivot Grid
  [Expand]Scheduler
  [Expand]Spreadsheet
  [Expand]Rich Text Editor
  [Expand]Tree List
  [Expand]Gauge Controls
  [Expand]Map Control
  [Expand]Layout Management
  [Expand]Windows Modern UI
  [Expand]Printing-Exporting
  [Expand]Data Editors
  [Expand]Navigation Controls
  [Expand]Spell Checker
  [Expand]Property Grid
  [Expand]PDF Viewer
  [Expand]TreeMap Control
  [Expand]Gantt Control
  [Expand]Diagram Control
  [Expand]Windows and Utility Controls
   Dialogs, Notifications and Panels
  [Expand]Scheduler (legacy)
 [Expand]Scaffolding Wizard
 [Expand]Localization
  Redistribution and Deployment
  Get More Help
 [Expand]API Reference
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Office File API
[Expand]Reporting
[Expand]Report and Dashboard Server
[Expand]Dashboard
[Expand]eXpressApp Framework
[Expand]eXpress Persistent Objects
[Expand]CodeRush
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation
View this topic on docs.devexpress.com (Learn more)

Applying Data Annotations

Expanded 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.v18.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.

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

Expanded Example

This example shows how to apply data annotation attributes to 'Product' class members.

Expanded See Also

Is this topic helpful?​​​​​​​