Setting the order of properties in an ExpandableObjectConverter

When inheriting from an ExpandableObjectConverter to display custom properties in a PropertyGrid, you will often want to set the order of the properties instead of having them in alphabetical order.

To do this you just need to override the GetProperties method. Call the Sort function on the PropertyDescriptorCollection object returned by the base function, passing an array of strings with the property names in the order that you desire.

For example:

public override PropertyDescriptorCollection
      GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
{
    return base.GetProperties(context, value, attributes)
                            .Sort(new string[] { "Name", "Street", "City", "Country" });
}

Any properties not in the string array will be added on afterwards in alphabetical order.

Creative Commons License
This work, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 License.
This entry was posted in .net, C#, Programming and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>