65.9K
CodeProject is changing. Read more.
Home

Add (Remove) Items to (from) PropertyGrid at Runtime

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.79/5 (46 votes)

Jan 12, 2005

1 min read

viewsIcon

197818

downloadIcon

6049

Modify PropertyGrid control Items collection, at runtime.

Sample Image - Dynamic_Propertygrid.jpg

Introduction

Hope you are familiar with Windows PropertyGrid control. I had used this control long time back, but I couldn't get a chance to work with dynamic properties. I thought there will be some methods like GridName.Items.Add() to add items at runtime. But, I had to write some lines of code to accomplish the same. My requirement was simple: I want to display all properties of my object in PropertyGrid, but I don't know the name of properties at design time. I have used CollectionBase, ICustomTypeDescriptor and PropertyDescriptor classes to accomplish the same.

Custom Property

First, I created one CustomProperty class with the following public properties:

  • Name
  • Value
  • ReadOnly
  • Visible

The constructor looks like this:

public CustomProperty(string sName, 
          object value, bool bReadOnly, bool bVisible )
{
 this.sName = sName;
 this.objValue = value;
 this.bReadOnly = bReadOnly;
 this.bVisible = bVisible;
}

Custom PropertyDescriptor

Next step is to create a PropertyDescriptor for my class. Derived CustomPropertyDescriptor from PropertyDescriptor class.

public class CustomPropertyDescriptor: PropertyDescriptor
{
 CustomProperty m_Property;
 public CustomPropertyDescriptor(ref CustomProperty myProperty, 
             Attribute [] attrs) :base(myProperty.Name, attrs)
 {
  m_Property = myProperty;
 }
 .....

We have to override all methods of PropertyDescriptor since it is an abstract class.

Custom Class

We will be creating object of this CustomClass for PropertyGrid.SelectedObject.

 public class CustomClass: CollectionBase,ICustomTypeDescriptor
{
  
 public void Add(CustomProperty Value)
 {
  base.List.Add(Value);
 }
  
 public void Remove(string Name)
 {
  foreach(CustomProperty prop in base.List)
  {
   if(prop.Name == Name)
   {
    base.List.Remove(prop);
    return;
   }
  }
 }
  
 public CustomProperty this[int index] 
 {
  get 
  {
   return (CustomProperty)base.List[index];
  }
  set
  {
   base.List[index] = (CustomProperty)value;
  }
 }
.......

Here is the most important method GetProperties, which will called while binding object to PropertyGrid.

 public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
 PropertyDescriptor[] newProps = new PropertyDescriptor[this.Count];
 for (int i = 0; i < this.Count; i++)
 {
  CustomProperty  prop = (CustomProperty) this[i];
  newProps[i] = new CustomPropertyDescriptor(ref prop, attributes);
 }
  return new PropertyDescriptorCollection(newProps);
}

How to use CustomClass

CustomClass can be used in any Windows Form having PropertyGrid.

CustomClass myProperties = new CustomClass();
propertyGrid1.SelectedObject = myProperties;
......

CustomProperty myProp = new CustomProperty(txtName.Text,txtValue.Text, 
                           chkReadOnly.Checked,true);
myProperties.Add(myProp);
propertyGrid1.Refresh();

Finally

I hope users can extend my CustomClass and use PropertyGrid control with dynamic properties.