Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 1.41 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Enum description in DataGridView
  2. enum DirectionEnum
  3. {
  4.     [Description("Right to left")]
  5.     rtl,
  6.  
  7.     [Description("Left to right")]
  8.     ltr
  9. }
  10. class Simple
  11. {
  12.     [DisplayName("Name")]
  13.     public string Name { get; set; }
  14.  
  15.     [DisplayName("Direction")]
  16.     public DirectionEnum dir { get; set; }
  17. }
  18. class DirectionDialog : Form
  19. {
  20.     public DirectionDialog()
  21.     {
  22.         DataGridView table = new DataGridView();
  23.         List<Simple> list = new List<Simple>(new Simple[]{
  24.             new Simple{ Name = "dave", dir = DirectionEnum.ltr},
  25.             new Simple{ Name = "dan", dir = DirectionEnum.rtl }
  26.         });
  27.         table.DataSource = list;
  28.         //view "rtl" or "ltr" in "Direction"
  29.         //I want "Right to left" or "Left to right:
  30.     }
  31. }
  32.        
  33. class Simple
  34. {
  35.     [DisplayName("Name")]
  36.     public string Name { get; set; }
  37.  
  38.     // Remove external access to the enum value
  39.     public DirectionEnum dir { private get; set; }
  40.  
  41.     // Add a new string property for the description
  42.     [DisplayName("Direction")]
  43.     public string DirDesc
  44.     {
  45.         get
  46.         {
  47.             System.Reflection.FieldInfo field = dir.GetType().GetField(dir.ToString());
  48.  
  49.             DescriptionAttribute attribute
  50.                     = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute))
  51.                         as DescriptionAttribute;
  52.  
  53.             return attribute == null ? dir.ToString() : attribute.Description;
  54.         }
  55.     }
  56. }