I would like to use data annotations on my classes and entities in a Windows Forms application (WinForms). I use windows DataGridViews and Infragistics UltraGrids. I have previously successfully used the [DisplayName("Name to Display")]
attribute to set the column header text on a DataGridView/UltraGrid.
This is very beneficial, because I may have several grids displaying this class, and instead of configuring each grid to display the appropriate header text, I can simply set one data annotation.
I'd like to make use of the following Data Annotations as well:
- Display
- [Display(AutoGenerateField=false)]
- Do not display this column
- [Display(Order=N)]
- Display this column as the nth column in the grid
- DisplayColumn
- [DisplayColumn("ColumnName")]
- If this object is a property of another object, display this column value instead of the object type
- Display Format
- [DisplayFormat(DataFormatString="{0:formatstring}")]
- Format the data using the specified format string
- DataType
- [DataType(DataType.Currency)]
- Display the data as a currency in the default local currency format
Example
Given the following annotated data classes:
public class Item
{
//Specifies that the column should not be displayed
[Display(AutoGenerateField = false)]
public int ItemID { get; set; }
//Specifies that the column should be the 1st column in the datagridview
[Display(Order = 1)]
public int Name { get; set; }
//Specifies that the column should be the 3rd column in the datagridview
//Specifies that the column header text should display "Cost" instead of "Price"
[Display(Order = 3, Name="Cost")]
//Specifies that the column should be rendered using the default local currency format string
[DataType(DataType.Currency)]
public int Price { get; set; }
//Specifies that the column should be the 4th column in the datagridview
[Display(Order = 4)]
//specifies that the column should be rendered using the datetime format string "M/d/yy h:mm tt"
[DisplayFormat(DataFormatString = "{0:M/d/yy h:mm tt")]
public DateTime ExpirationDate { get; set; }
//Specifies that the column should be the 2nd column in the datagridview
[Display(Order = 2)]
public ItemCategory Category { get; set; }
}
//Specifies that the Name column should be displayed, if referenced in a containing object
[DisplayColumn("Name")]
public class ItemCategory
{
public int CategoryID { get; set; }
public string Name { get; set; }
}
I would like the DataGridView to render like this:
+-------+---------------+--------+-----------------+
| Name | Category | Cost | ExpirationDate |
+-------+---------------+--------+-----------------+
| Item1 | Category1Name | $30.45 | 7/23/17 5:22 PM |
+-------+---------------+--------+-----------------+
| Item2 | Category1Name | $45.05 | 8/24/17 6:22 PM |
+-------+---------------+--------+-----------------+
| Item3 | Category2Name | $35.50 | 9/25/17 7:22 PM |
+-------+---------------+--------+-----------------+
However in practice with DataGridViews in .Net 4.5.2 WinForms, the datagrid is actually displayed as follows:
+----+-------+-------+----------------+--------------------+
| ID | Name | Price | ExpirationDate | Category |
+----+-------+-------+----------------+--------------------+
| 1 | Item1 | 30.45 | 7/23/17 | Namespace.Category |
+----+-------+-------+----------------+--------------------+
| 2 | Item2 | 45.05 | 8/24/17 | Namespace.Category |
+----+-------+-------+----------------+--------------------+
| 3 | Item3 | 35.50 | 9/25/17 | Namespace.Category |
+----+-------+-------+----------------+--------------------+
Documentation
The documentation declares that it is supported in ASP.NET and ASP.NET MVC.
System.ComponentModel.DataAnnotations Namespace
The System.ComponentModel.DataAnnotations namespace provides attribute classes that are used to define metadata for ASP.NET MVC and ASP.NET data controls.
Questions
It seems that these classes have not been adopted/supported in the windows forms environment. Is this true?
Is there a simple way to implement support for the data annotations in WinForms?
Is there a simple way to annotate the classes/entities that can be used to format the display of a DataGridView and/or UltraGrid?