Changing the display name and sorting properties of the Property Grid
This post assumes a bit of background knowledge on how to use and control the property grid when displaying the properties of an object.
The property grid is a great control, I like it a lot, but it does have some problems. One of these is the use of attributes to specify and control categories, display names and descriptions of each property it displays. The attributes, which are specified at design time, allow only string literals to be set. These are compiled into assembies metadata and because of this they cannot be changed at runtime. So, for example, you cannot look up the name or description of a property from a database, or a file, or even change it depending on a condition.
Another problem is there are no attributes to specify in which order the property grid should displays the object properties, so even though in code the properties of your object are defined in the order you want, the property grid won't display them in that order.
There are two classes that control how the property grid displays an objects properties that help fix these two problems, one class is used to look up the display name, category and description of a property while the other class indentifies which properties of the object, and more importantly in what order, these properties should be displayed.
The class System.ComponentModel.PropertyDescriptor is a class that tells the property grid how to describe a property. There are many methods and properties that need to be overriden but the three most useful are the read only DisplayName, Category and the Description properties. This descriptor class could be thought of as a class that describes how a single property is displayed in the property grid.
The second class allows you to select and sort the properties displayed on the property grid. The System.Component.CustomTypeDescriptor class allows to control how the properties of a type are displayed. It's main task is to build a collection of PropertyDescriptors.
If the PropertyDescriptor describes how each individual property of a type is displayed then the CustomTypeDescriptor completes the picture by allowing a type to describe how it's properties are displayed. As you can imagine the CustomTypeDescriptor creates instances of the PropertyDescriptor. Generally the object whos properties you want to display is inherited from the CustomTypeDescriptor and this allows the object to describe itself to the property grid.
Granted this description, without code samples, isn't particularly intuitive so here is a small sample application that demonstrates how to use the above two classes. The example displays a simple Employee class in the property grid. The employee class has no attributes describing how each of it's properties are displayed, that information is extracted from a comma delimited file by a EmployeePropertyDescriptor class, which doesn't do a very efficient job of the lookup, but it is only a demo. The employee class also inherits from the CustomTypeDescriptor and in the GetProperties() method specifies the order of the properties and using that order builds a collection of EmployeePropertyDescriptor objects which is used by the property grid. Very simple example.
DisplayName And Sorting PropertyGrid.zip
Very sorry !! Missed out a very small but important detail! You have to set the Property Grids PropertySort property (eh?) to Categorized. Otherwise you'll still get the list in alphabetic order. Sorry for missing that.