Use PropertyGrid descriptions and categories in C#

Use PropertyGrid descriptions and categories

This example extends the post Use a PropertyGrid to let the user edit objects in C# to use PropertyGrid descriptions and categories. Just give a class’s properties the Description and Category attributes to make the PropertyGrid use them in its display.

In the picture on the right, the Person object’s properties are grouped into the Address, Contact Info, and Name categories. The picture also shows the description for the currently selected property, Email.

To use these attributes more easily, include the System.ComponentModel namespace in a using statement.

using System.ComponentModel;

Next place the Description and Category attributes before the properties. The following code shows the FirstName property’s definition with these attributes.

private string _FirstName;
[Description("The person's first or given name")]
[Category("Name")]
public string FirstName
{
    get
    {
        return _FirstName;
    }
    set
    {
        _FirstName = value;
    }
}

Now the PropertyGrid automatically groups properties by category and displays their descriptions.


Download Example   Follow me on Twitter   RSS feed




This entry was posted in attributes, controls, OOP and tagged , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *