Click here to Skip to main content
13,298,655 members (48,445 online)
Click here to Skip to main content
Add your own
alternative version


69 bookmarked
Posted 7 Jan 2005

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

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Mazen el Senih25-Jun-12 5:01
memberMazen el Senih25-Jun-12 5:01 
GeneralMy vote of 1 Pin
SuperToha29-Mar-10 22:21
memberSuperToha29-Mar-10 22:21 
GeneralRe: My vote of 1 Pin
FernandaUY5-Jan-12 20:13
memberFernandaUY5-Jan-12 20:13 
GeneralThank you a lot Pin
Nasenbaaer4-Feb-10 14:14
memberNasenbaaer4-Feb-10 14:14 
GeneralQuastion Pin
yura19894-Nov-09 3:27
memberyura19894-Nov-09 3:27 
GeneralMy vote of 1 Pin
progressma28-Aug-09 4:58
memberprogressma28-Aug-09 4:58 
GeneralRe: My vote of 1 Pin
FernandaUY5-Jan-12 20:14
memberFernandaUY5-Jan-12 20:14 
QuestionSerializing/Deserializing? Pin
DJB MASTER15-Feb-09 1:49
memberDJB MASTER15-Feb-09 1:49 
GeneralThanks Pin
noi_loanxmen10-Jun-08 20:01
membernoi_loanxmen10-Jun-08 20:01 
GeneralU're the man! Pin
pedromonica24-Oct-07 6:18
memberpedromonica24-Oct-07 6:18 
GeneralMany thanks!! Pin
Marcelo Ricardo de Oliveira25-Jun-07 12:35
memberMarcelo Ricardo de Oliveira25-Jun-07 12:35 
GeneralThanks! Pin
Hadas_F30-May-07 5:44
memberHadas_F30-May-07 5:44 
GeneralUpdate PropertyGrid Pin
berilium19-Sep-05 7:07
memberberilium19-Sep-05 7:07 
QuestionStrange - no categories? Pin
mathewww16-Jul-05 10:56
membermathewww16-Jul-05 10:56 
Answernevermind ...Re: Strange - no categories? Pin
mathewww16-Jul-05 11:09
membermathewww16-Jul-05 11:09 
GeneralRe: nevermind ...Re: Strange - no categories? Pin
Matteo Ermidoro17-Jul-05 12:06
memberMatteo Ermidoro17-Jul-05 12:06 
QuestionWhy not just use ICustomTypeDescriptor?? Pin
versat147417-Jan-05 19:01
memberversat147417-Jan-05 19:01 
AnswerRe: Why not just use ICustomTypeDescriptor?? Pin
Matteo Ermidoro18-Jan-05 22:13
memberMatteo Ermidoro18-Jan-05 22:13 
GeneralRe: Why not just use ICustomTypeDescriptor?? Pin
Dan Radu19-Nov-07 15:47
memberDan Radu19-Nov-07 15:47 
Generalnice Pin
NicoRi13-Jan-05 21:59
memberNicoRi13-Jan-05 21:59 
GeneralRe: nice Pin
Matteo Ermidoro14-Jan-05 3:58
memberMatteo Ermidoro14-Jan-05 3:58 
GeneralRe: nice Pin
NicoRi14-Jan-05 5:51
memberNicoRi14-Jan-05 5:51 
GeneralRe: nice Pin
Matteo Ermidoro15-Jan-05 7:41
memberMatteo Ermidoro15-Jan-05 7:41 
GeneralRe: nice Pin
NicoRi15-Jan-05 9:13
memberNicoRi15-Jan-05 9:13 
GeneralRe: nice Pin
Anonymous16-Jan-05 23:41
sussAnonymous16-Jan-05 23:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171207.1 | Last Updated 7 Jan 2005
Article Copyright 2005 by Matteo Ermidoro
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid