Click here to Skip to main content
Click here to Skip to main content

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

, 12 Jan 2005
Rate this:
Please Sign up or sign in to vote.
Modify PropertyGrid control Items collection, at runtime.

Sample Image - Dynamic_Propertygrid.jpg


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)
 public void Remove(string Name)
  foreach(CustomProperty prop in base.List)
   if(prop.Name == Name)
 public CustomProperty this[int index] 
   return (CustomProperty)base.List[index];
   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, 


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


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
India India
Sreejumon, Microsoft MVP for last 5 years.

He blogs at

He maintains the following sites.

Comments and Discussions

GeneralMy vote of 5 PinmemberTarek.Eladly13-Feb-13 1:36 
GeneralMy vote of 5 PinmemberThorsten Bruning21-Dec-12 2:32 
GeneralMy vote of 5 Pinmemberoozakala4-Dec-12 7:53 
GeneralMy vote of 5 PinmemberVolynsky Alex7-Jun-12 6:41 
GeneralLabview can use PropertyGrid thanks to your code Pinmemberpcncottin30-Jul-08 19:11 
QuestionDatatype sensitve properties Pinmemberharish_somanchi22-Mar-07 11:05 
GeneralThere is a small bug inside the code Pinmemberalp ates26-Dec-06 5:04 
I have tried to use this class in a propertygrid but it failed. It returns real properties instead of the created properties. Here is the reason,

there is 2 calling type for getproperties function, the one
public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
works fine but the other which is used by the propertygrid
public PropertyDescriptorCollection GetProperties()
fails to return thr correct property descriptors.

So i have changed the code in the property grid to first function by calling with an empty attribute.

But rest works fine. Thank you
GeneralType PinmemberLions_italy16-Jan-06 22:28 
QuestionIs it possible to change [Readonly( )] attribute at runtime? Pinmemberjjohn20056-Dec-05 23:13 
AnswerRe: Is it possible to change [Readonly( )] attribute at runtime? PinmemberLocusta7411-Feb-07 19:40 
GeneralLook a little further PinmemberByteGhost28-Feb-05 12:38 
GeneralDynamic list property PinmemberPoletto30-Jan-05 23:23 
GeneralRe: Dynamic list property Pinmembersalle7824-Nov-08 3:27 
GeneralAttributes Pinmemberdamslang24-Jan-05 23:25 
GeneralRe: Attributes Pinmemberdamslang25-Jan-05 0:30 
GeneralExtending dynamic propertygrid capabilities PinmemberPoletto24-Jan-05 22:56 
GeneralRe: Extending dynamic propertygrid capabilities Pinmembervarad273-Sep-07 22:20 
GeneralRe: Extending dynamic propertygrid capabilities PinmemberClayton Hotson18-Mar-10 6:54 

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
Web01 | 2.8.150414.1 | Last Updated 12 Jan 2005
Article Copyright 2005 by Sreejumon
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid