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

Dynamic Properties in the PropertyGrid

, 7 Jan 2005
Rate this:
Please Sign up or sign in to vote.
How to modify the properties shown in a propertygrid at runtime.

Sample Image - DynPropGrid.jpg


The article is mainly to explain how to make PropertyGrid show only some properties of a class. One of the big limitations of this control is that it's not possible to hide or show any class properties at runtime.

For example, in an application that I'm developing, I had to show in the PropertyGrid a class that inherits from XMLElement, but I didn't want all the properties of the XMLElement to be shown to the user. One solution was to create a new class that makes access to the main class, but this solution was too less expandable and not useful at runtime. So, I created this control that lets me control the properties shown in the PropertyGrid at runtime too.

Using the code

The class to be shown in the PropertyGrid must inherit from ICustomClass which has only one property, PublicProperties. This is a collection of myPropertys. myProperty implements two properties:

  • Name: the name of the property to be shown.
  • Category: the name of the category to be shown in the Propertygrid.
interface ICustomClass
    PropertyList PublicProperties

Adding a new property in the Propertygrid is simple:

c.PublicProperties.Add(new myProperty("test3","Category1"));

In this line, we want to show in the PropertyGrid the property "test3" in the category "Category1".

Now, to remove the property from the control:


Now, let's check the PropertyGrid. The properties are visualized in the PropertyTab object. So it's necessary to create a new PropertyTab to access the visualized rows. To filter out the properties visualized, I've to override the getProperties method of the PropertyTab class.

This method has an input parameter, the object to be shown in the PropertyGrid. And this object must implement the ICustomClass interface so that I can get the properties I want to display in the PropertyGrid within the GetProperties method.

To get all the properties of the selected object, there's the TypeDescriptor.GetProperties(component). This function returns a PropertyDescriptorCollection filled with all the properties of the class to be shown. But, as I said before, the class to be shown in the PropertyGrid must implement ICustomClass.

So with this row:

//Componet must implement the ICUSTOMCLASS interface.
ICustomClass bclass=(ICustomClass)component;

it's possible to retrieve the list of properties to be shown in the PropertyTab. It's necessary to create a new PropertyDescriptorfor for each new property.

PropertyDescriptor[] propArray = 
        new PropertyDescriptor[bclass.PublicProperties.Count];

Now I've to create the new property that will be shown in the PropertyGrid. So I used TypeDescriptor.CreateProperty from System.ComponentModel.

for (int i=0;i<bclass.PublicProperties.Count;i++)
     //Find the list of properties in the array of properties 
     //whose name is in the PubliCProperties
     PropertyDescriptor prop=
     //Build a new properties
     arrProp[i] = TypeDescriptor.CreateProperty(prop.ComponentType, prop, 
            new CategoryAttribute(bclass.PublicProperties[i].Category));

At the end of the loop, the arrProp array will be filled up with the new property to be shown in the PropertyTab. Now, since Getproperties has a PropertyDescriptorCollection as return parameter, we can create a new instance of it with:

new PropertyDescriptorCollection(arrProp);

The last step to get everything working, is to override the creation of the PropertyTab of the PropertyGrid.

protected override PropertyTab CreatePropertyTab(Type tabType)

In this function, the base creation of the Propertytab is bypassed, and only the filtered Propertytab is created.


  • Original article.


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

Matteo Ermidoro
Web Developer
Italy Italy
No Biography provided

Comments and Discussions

GeneralMy vote of 5 PinmemberMazen el Senih25-Jun-12 5:01 
GeneralMy vote of 1 PinmemberSuperToha29-Mar-10 22:21 
GeneralRe: My vote of 1 PinmemberFernandaUY5-Jan-12 20:13 
GeneralThank you a lot PinmemberNasenbaaer4-Feb-10 14:14 
GeneralQuastion Pinmemberyura19894-Nov-09 3:27 
GeneralMy vote of 1 Pinmemberprogressma28-Aug-09 4:58 
GeneralRe: My vote of 1 PinmemberFernandaUY5-Jan-12 20:14 
QuestionSerializing/Deserializing? PinmemberDJB MASTER15-Feb-09 1:49 
GeneralThanks Pinmembernoi_loanxmen10-Jun-08 20:01 
GeneralU're the man! Pinmemberpedromonica24-Oct-07 6:18 
GeneralMany thanks!! PinmemberMarcelo Ricardo de Oliveira25-Jun-07 12:35 
GeneralThanks! PinmemberHadas_F30-May-07 5:44 
GeneralUpdate PropertyGrid Pinmemberberilium19-Sep-05 7:07 
QuestionStrange - no categories? Pinmembermathewww16-Jul-05 10:56 
Answernevermind ...Re: Strange - no categories? Pinmembermathewww16-Jul-05 11:09 
GeneralRe: nevermind ...Re: Strange - no categories? PinmemberMatteo Ermidoro17-Jul-05 12:06 
QuestionWhy not just use ICustomTypeDescriptor?? Pinmemberversat147417-Jan-05 19:01 
AnswerRe: Why not just use ICustomTypeDescriptor?? PinmemberMatteo Ermidoro18-Jan-05 22:13 
GeneralRe: Why not just use ICustomTypeDescriptor?? PinmemberDan Radu19-Nov-07 15:47 
Generalnice PinmemberNicoRi13-Jan-05 21:59 
GeneralRe: nice PinmemberMatteo Ermidoro14-Jan-05 3:58 
GeneralRe: nice PinmemberNicoRi14-Jan-05 5:51 
GeneralRe: nice PinmemberMatteo Ermidoro15-Jan-05 7:41 
GeneralRe: nice PinmemberNicoRi15-Jan-05 9:13 
GeneralRe: nice PinsussAnonymous16-Jan-05 23:41 
GeneralRe: nice PinmemberMatteo Ermidoro16-Jan-05 23:45 
GeneralRe: nice PinmemberNicoRi17-Jan-05 0:40 
GeneralRe: nice PinmemberMatteo Ermidoro18-Jan-05 22:14 

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