RSS

Bind class object instances using PropertyGrid

15 Sep

1. Design a class with public field

2. Add attributes to the class and class members

3. Add a PropertyGrid control to a form, and set the “SelectedObject” Property to an instance of an object, that is it. A nice thing is that it is automatically data bound to the object instance if you edit the value through the grid.

4. Some commonly used attributes:

  • Category
  • DisplayName
  • Description
  • DefaultValue
  • PasswordPropertyText
  • Browsable
  • ReadOnly
  • DefaultProperty

More details, refer to MSDN here.

5. In case some class members are other class instances embedded, for instance

class ParentClass
{
public SomeChildClass AnotherChild
{get; set;}
}

Now if you want to display the detailed info of SomeChildClass in the grid as well, add the following attributes:

[TypeConverter(typeof(ExpandableObjectConverter))]
class ParentClass
{
public SomeChildClass AnotherChild
{get; set;}
}

Advertisements
 
Leave a comment

Posted by on September 15, 2009 in Dotnet/C#

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: