1

I am creating a Windows Form Application in C# and using property grid in it. I am displaying properties of a Person class object in it and wanted to use Display data annotation to set property's description, display order of properties and other things. To implement the same I have added reference to System.ComponentModel.DataAnnotations in my code and code for my Person class is like this:

class Person
    {
        [Display(Name = "First Name", Order = 1,
        Prompt = "Enter First Name", Description = "Person First Name")]
        public string firstname { get; set; }
        [Display(Name = "Last Name", Order = 2,
        Prompt = "Enter Last Name", Description = "Person Last Name")]
        public string lastname { get; set; }
        [Display(Name = "Age", Order = 3,
        Prompt = "Enter Age", Description = "Person Age")]
        public int age { get; set; }
    }

But data annotations not working. Here is screenshot of property grid in my app.. screenshot of property grid

I am not able to understand why data annotation are not working in my code. Can anyone help out? Is there anything I am missing out? Do I need to implement any code segment to make them work?

Dexter404
  • 25
  • 3
  • 6

2 Answers2

2

Seems to work if you use the attributes from System.ComponentModel namespace, for example:

[Description("This is the First Name")] 
[DisplayName("First Name")]       
public string firstname { get; set; }
Etienne
  • 1,058
  • 11
  • 22
  • But any suggestion or clue why Display annotation is not working? :/ – Dexter404 Aug 02 '17 at 05:05
  • not specifically sorry, have a read at this : https://stackoverflow.com/questions/15051298/list-of-propertygrid-attributes – Etienne Aug 02 '17 at 05:19
  • this is also a good link for you as it shows use of attributes https://www.codeproject.com/Articles/22717/Using-PropertyGrid – Etienne Aug 02 '17 at 05:20
1

Set the property PropertySort of your propertyGrid to PropertySort.NoSort and properties will be displayed in the order they are declared

propertyGrid1.PropertySort = PropertySort.NoSort;
ClearLogic
  • 3,616
  • 1
  • 23
  • 31