Click here to Skip to main content
11,709,955 members (73,615 online)
Click here to Skip to main content

Ordering Items in the Property Grid

, 5 Apr 2004 CPOL 119.5K 4K 75
Rate this:
Please Sign up or sign in to vote.
A simple custom attribute to order properties in the PropertyGrid.

Sample screenshot

Introduction

The PropertyGrid Component that ships with Visual Studio is a great way to add configurability to an application with very little effort. An object's properties are displayed when the object is assigned to the grid's SelectedObject property. The properties are displayed in either alphabetic order, or in categorized alphabetic order. Unfortunately, there is no built-in functionality to specify any other order. This article presents some simple code which allows the order of properties to be specified via a custom attribute in the same form as the grid's other attributes.

Using the code

Properties in code can be decorated with attributes to group them in categories when shown in the Property Grid. Descriptions can be applied in a similar way. The attributes are extracted via reflection when the object is assigned to the grid.

For example, using the following code -

[DefaultProperty("Name")]
public class Person
{
    protected const string PERSONAL_CAT = "Personal Details";
    
    private string _name = "Bob";
    private DateTime _birthday = new DateTime(1975,1,1);

    [Category(PERSONAL_CAT)]
    public string Name
    {
        get {return _name;} 
        set {_name = value;} 
    }

    [Category(PERSONAL_CAT)]
    public DateTime Birthday
    {
        get {return _birthday;}
        set {_birthday = value;}
    }

    [Category(PERSONAL_CAT)]
    public int Age
    {
        get 
        {
            TimeSpan age = DateTime.Now - _birthday; 
            return (int)age.TotalDays / 365; 
        }
    }
}

an instantiated object assigned to the PropertyGrid will look like this -

Sample screenshot

The name, birthday and age are shown in the "Personal Details" category as expected. The name and birthday are editable as they have gets & sets. The age is read-only. The name is highlighted as the default property. This is fine in development, but a client is unlikely to want the key piece of information, i.e. the name, coming last due to the alphabetic order. The alphabetic ordering will also be applied to the combined properties in objects derived from the Person class. Ideally, we want the ordering to be- 'name', then 'birthday', then 'age'. Any additional properties added in derived classes should follow from these, or be placed in another category.

Enter the Property Sorter. With some additional attributes, and the inclusion of the PropertySorter.cs file in the project, the ordering can be defined. The Person class is modified to look like this -

[TypeConverter(typeof(PropertySorter))]
[DefaultProperty("Name")]
public class Person
{
    protected const string PERSONAL_CAT = "Personal Details";
    
    private string _name = "Bob";
    private DateTime _birthday = new DateTime(1975,1,1);

    [Category(PERSONAL_CAT), PropertyOrder(10)]
    public string Name
    {
        get {return _name;} 
        set {_name = value;} 
    }

    [Category(PERSONAL_CAT), PropertyOrder(11)]
    public DateTime Birthday
    {
        get {return _birthday;}
        set {_birthday = value;}
    }

    [Category(PERSONAL_CAT), PropertyOrder(12)]
    public int Age
    {
        get 
        {
            TimeSpan age = DateTime.Now - _birthday; 
            return (int)age.TotalDays / 365; 
        }
    }
}

The only changes are the addition of the TypeConverter attribute at the top, and the PropertyOrder attributes on each property. When the class is instantiated and assigned to the PropertyGrid, it looks like this -

Person Class Ordered

Furthermore, any derived classes can have PropertyOrder attributes assigned and they will be honored. The order value passed in the attribute signifies the rank of the property in a sparse array (i.e. non-contiguous). Categories can be added on 'hundreds' boundaries (100, 200, 300 etc.) to allow subsequent categories to be added without breaking previous code. (Doesn't that remind you of line numbers in Basic?)

Points of Interest

The code to do the ordering is very simple, and not really worth describing. Have a look at PropertySorter.cs to see how it works. I did like the use of dynamic code compilation in the demo project to dynamically compile the examples, and allow you to change and test them immediately (go on try it!). Maybe, this will result in another CP project for a CP demo harness?

Conclusion

This is some very simple code to fix a hole in PropertyGrid's armory. Being able to specify the order of properties using attributes makes your data look better. The demo application provides some code samples that are dynamically compiled, so you can change the code and see the effects immediately. I hope someone finds this useful!

History

First CP project.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Paul Tingey
Software Developer (Senior)
New Zealand New Zealand
Working with code since 1994. Mostly c#, with history in VC++, Embedded C++, Delphi, and Modular 2. Working in the finanical sector.

You may also be interested in...

Comments and Discussions

 
GeneralMy Vote of 5 Pin
trantrum17-Oct-13 19:52
membertrantrum17-Oct-13 19:52 
GeneralMy vote of 5 Pin
toATwork25-Jun-13 5:14
membertoATwork25-Jun-13 5:14 
GeneralMy vote of 3 Pin
cackeus9-Apr-13 0:41
membercackeus9-Apr-13 0:41 
GeneralMy vote of 5 Pin
Thorsten Bruning21-Dec-12 4:28
memberThorsten Bruning21-Dec-12 4:28 
QuestionIssue with MergableProperty and multiple objects selected Pin
Gnail12-Jul-12 20:28
memberGnail12-Jul-12 20:28 
AnswerRe: Issue with MergableProperty and multiple objects selected Pin
cackeus9-Apr-13 0:40
membercackeus9-Apr-13 0:40 
GeneralMy vote of 5 Pin
MarkLTX27-Mar-12 4:56
memberMarkLTX27-Mar-12 4:56 
Solves my problem. Easy to use.
QuestionConflicts with existing TypeConverter Pin
MarkLTX26-Mar-12 5:03
memberMarkLTX26-Mar-12 5:03 
GeneralMy vote of 5 Pin
ScruffyDuck19-Oct-11 10:53
memberScruffyDuck19-Oct-11 10:53 
GeneralMy vote of 5 Pin
KPetrosyan26-Apr-11 1:09
memberKPetrosyan26-Apr-11 1:09 
GeneralMy vote of 5 Pin
Eddy Vluggen21-Mar-11 9:43
memberEddy Vluggen21-Mar-11 9:43 
GeneralNice Work Pin
infosarang16-Jun-10 18:36
memberinfosarang16-Jun-10 18:36 
Generalmodification to allow auto ordering Pin
Mike Manfredi28-Apr-10 4:51
memberMike Manfredi28-Apr-10 4:51 
GeneralIn multiple Categories, the sort does not work... Pin
ze_tedesco6-Nov-07 2:25
memberze_tedesco6-Nov-07 2:25 
GeneralVery nice solution Pin
Dave1975199512-Oct-07 4:29
memberDave1975199512-Oct-07 4:29 
GeneralRe: Very nice solution Pin
AloneSoul24-Oct-07 21:00
memberAloneSoul24-Oct-07 21:00 
QuestionOrder first by category and then the property order? Pin
saho25-Sep-07 5:55
membersaho25-Sep-07 5:55 
GeneralVB translation Pin
TWallick19-Sep-07 11:18
memberTWallick19-Sep-07 11:18 
GeneralMultiple selection Pin
nedoma29-Jul-07 4:20
membernedoma29-Jul-07 4:20 
GeneralThanks , and one comment Pin
caddzooks28-Jun-07 23:01
membercaddzooks28-Jun-07 23:01 
General3 years later and still AWESOME Pin
CrankItOut14-Mar-07 12:17
memberCrankItOut14-Mar-07 12:17 
GeneralFantastic Pin
Ben Ratzlaff7-Feb-07 9:04
memberBen Ratzlaff7-Feb-07 9:04 
GeneralExcellent! Pin
Marc Clifton9-Jul-06 6:55
protectorMarc Clifton9-Jul-06 6:55 
QuestionHow set order dynamically? Pin
okcode3-Jun-06 21:21
memberokcode3-Jun-06 21:21 
Generalerror-entry point not defined & directive missing Pin
sasire183-Feb-06 1:46
membersasire183-Feb-06 1:46 
GeneralRe: error-entry point not defined & directive missing Pin
willydemis6-Oct-07 10:24
memberwillydemis6-Oct-07 10:24 
GeneralEliminating items Pin
jstiff19-Aug-04 21:14
memberjstiff19-Aug-04 21:14 
GeneralCategory Ordering Pin
Ryan McFarren14-Jun-04 4:30
memberRyan McFarren14-Jun-04 4:30 
GeneralRe: Category Ordering Pin
Ryan McFarren27-Apr-05 7:15
memberRyan McFarren27-Apr-05 7:15 
GeneralThanks! Pin
Rick Pingry13-Jun-04 12:08
memberRick Pingry13-Jun-04 12:08 
GeneralRe: Thanks! Pin
alias4710-Aug-07 1:37
memberalias4710-Aug-07 1:37 
AnswerRe: Thanks! Pin
Rick Pingry10-Aug-07 5:06
memberRick Pingry10-Aug-07 5:06 
GeneralRe: Thanks! Pin
alias4710-Aug-07 11:45
memberalias4710-Aug-07 11:45 
GeneralOne question about read only Pin
jason_htun6-May-04 21:08
memberjason_htun6-May-04 21:08 
GeneralRe: One question about read only Pin
Paul T10-May-04 22:24
memberPaul T10-May-04 22:24 
GeneralRe: One question about read only Pin
jason_htun10-May-04 23:02
memberjason_htun10-May-04 23:02 
GeneralVery useful - thanx Pin
Jack Modulator12-Apr-04 11:22
memberJack Modulator12-Apr-04 11:22 
GeneralImages Pin
Heath Stewart6-Apr-04 10:47
editorHeath Stewart6-Apr-04 10:47 
GeneralRe: Images Pin
Paul T6-Apr-04 11:52
memberPaul T6-Apr-04 11:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150819.1 | Last Updated 6 Apr 2004
Article Copyright 2004 by Paul Tingey
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid