Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
195 views
Welcome To Ask or Share your Answers For Others

1 Answer

DataAnnotations attributes for DataGridView in Windows Forms

There is no built-in support for Data Annotation in Windows Forms, but knowing how the attributes work and how windows forms work, we can use them in windows forms.

Here in this post, I'll show an extension method for DataGridView which binds an IList<T> to DataGridView and auto-generate columns based on the data annotations attributes, so you can get the following DataGridView, by calling dataGridView1.Bind(list);:

Look at the following items which are coming from data annotations attributes:

  • Column visibility: Id column is invisible
  • Column header texts: They are custom texts different from property names
  • Order of columns: The order of columns is custom, different from property orders
  • Tooltip: We have shown custom tooltip for columns.
  • Format of data: We have used custom format for date.

And many more things still you can have using attributes.

enter image description here

While the model is like this:

using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

[TypeDescriptionProvider(typeof(MetadataTypeTypeDescriptionProvider))]
public class Person
{
    [Display(Name = "Id")]
    [Browsable(false)]
    public int? Id { get; set; }

    [Display(Name = "First Name", Description = "First name.", Order = 1)]
    public string FirstName { get; set; }

    [Display(Name = "Last Name", Description = "Last name", Order = 2)]
    public string LastName { get; set; }

    [Display(Name = "Birth Date", Description = "Date of birth.", Order = 4)]
    [DisplayFormat(DataFormatString = "yyyy-MM-dd")]
    public DateTime BirthDate { get; set; }

    [Display(Name = "Homepage", Description = "Url of homepage.", Order = 5)]
    public string Url { get; set; }

    [Display(Name = "Member", Description = "Is member?", Order = 3)]
    public bool IsMember { get; set; }
}

Bind<T> Extension Method

using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Windows.Forms;

public static class DataGridViewExtensions
{
    public static void Bind<T>(this DataGridView grid, IList<T> data,
        bool autoGenerateColumns = true)
    {
        if (autoGenerateColumns)
        {
            var properties = TypeDescriptor.GetProperties(typeof(T));
            var metedata = properties.Cast<PropertyDescriptor>().Select(p => new
            {
                Name = p.Name,
                HeaderText = p.Attributes.OfType<DisplayAttribute>()
                    .FirstOrDefault()?.Name ?? p.DisplayName,
                ToolTipText = p.Attributes.OfType<DisplayAttribute>()
                    .FirstOrDefault()?.GetDescription() ?? p.Description,
                Order = p.Attributes.OfType<DisplayAttribute>()
                    .FirstOrDefault()?.GetOrder() ?? int.MaxValue,
                Visible = p.IsBrowsable,
                ReadOnly = p.IsReadOnly,
                Format = p.Attributes.OfType<DisplayFormatAttribute>()
                    .FirstOrDefault()?.DataFormatString,
                Type = p.PropertyType
            });
            var columns = metedata.OrderBy(m => m.Order).Select(m =>
            {
                DataGridViewColumn c;
                if (m.Type == typeof(bool)) {
                    c = new DataGridViewCheckBoxColumn(false); }
                else if (m.Type == typeof(bool?)) {
                    c = new DataGridViewCheckBoxColumn(true); }
                else { c = new DataGridViewTextBoxColumn(); }
                c.DataPropertyName = m.Name;
                c.Name = m.Name;
                c.HeaderText = m.HeaderText;
                c.ToolTipText = m.ToolTipText;
                c.DefaultCellStyle.Format = m.Format;
                c.ReadOnly = m.ReadOnly;
                c.Visible = m.Visible;
                return c;
            });
            grid.Columns.Clear();
            grid.Columns.AddRange(columns.ToArray());
        }
        grid.DataSource = data;
    }
}

DataAnnotations Validation attributes for Windows Forms

Also for supporting data annotations validations you can implement IDataErrorInfo interface using Validator class, the same way that I've done it in DataAnnotations Validation attributes for Windows Forms.

Note

To enhance the answer, you may want to creating a type descriptor which cares about metadata attributes and then decorate the models with that type descriptor. You can start by using the code of AssociatedMetadataTypeTypeDescriptor, MetadataPropertyDescriptorWrapper, AssociatedMetadataTypeTypeDescriptionProvider.

Also you can create a Metadata class and apply the effect of some attributes like Url or DataType on the metadata. Looking into this post will give you some idea: Combining multiple Attributes to a single Attribute - Merge Attributes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...