Make a type converter to let the PropertyGrid display and edit compound properties in C#

The example Use a PropertyGrid control to let the user edit program objects in C# shows how to use the PropertyGrid control to let the user view and edit simple object properties. However, if a property is an object itself, then the PropertyGrid displays whatever the object's ToString method returns and doesn't allow the user to edit it.

To let the PropertyGrid display and edit the value properly, you can provide a type converter class for that object type. This example uses a Person class that has a property named Address of type StreetAddress. The StreetAddress class has Street, City, State, and Zip properties.

To make a type converter, first decorate the class that you will convert with the TypeConverter attribute defined in the System.ComponentModel namespace. This class uses the StreetAddressConverter class to convert StreetAddress objects to and from strings so the StreetAddress class's declaration looks like the following:

using System.ComponentModel;

namespace howto_make_type_converter
{
[TypeConverter(typeof(StreetAddressConverter))]
class StreetAddress
{
...
}
}

Next make the type converter class. It should inherit from the TypeConverter class, also defined in the System.ComponentModel namespace.

using System.ComponentModel;

namespace howto_make_type_converter
{
class StreetAddressConverter : TypeConverter
{
...
}
}

This class should override several methods to give the PropertyGrid the tools it needs to display and edit properties of the type StreetAddress. These methods are:

Method Purpose
CanConvertFrom Returns true if the indicated type is string to indicate that the converter can create a StreetAddress from a string.
CanConvertTo Returns true if the indicated type is string to indicate that the converter can convert a StreetAddress into a string.
ConvertFrom Converts a string into a StreetAddress.
ConvertTo Converts a StreetAddress into a string.
GetProperties Returns a collection of type descriptors to tell the PropertyGrid about the properties that the StreetAddress supports.

The code for these methods fairly straightforward but somewhat long so it's not shown here. Download the example to see the code.

To make converting a StreetAddress into a string, this version overrides its ToString method to return the object's properties separated by commas. The converter's ConvertTo method simply calls it.

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments

  • 4/4/2012 7:38 AM Dennis wrote:
    Really great article!

    Excactly what I was looking for.

    Just a clear description of what to do when using property grids with own types.

    Thank you very much for sharing! :)
    Reply to this
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.