Click here to Skip to main content
11,932,461 members (50,637 online)
Click here to Skip to main content
Add your own
alternative version


76 bookmarked

Filtering properties in a PropertyGrid

, 27 Mar 2006
Rate this:
Please Sign up or sign in to vote.
This articles describes some easy ways to filter the properties displayed in a Microsoft PropertyGrid.

Sample Image - FilteredPropertyGrid.jpg


The wonderful PropertyGrid control, provided by Microsoft, is a very useful component for editing settings or parameters in your application. You can easily let the user customize any object at runtime. When you pass an object to the SelectedObject property of a PropertyDataGrid, the PropertyDataGrid will display all the properties of your object that has the BrowsableAttribute set to true. It is both very simple and efficient.

But sometimes, you only want to expose just some properties, not all of them. Suppose you want to allow the user of your application to edit the appearance of your main form. How would you do that ? In fact, this can be done very easily. Microsoft thought about this matter and gave us a very interesting property, named BrowsableAttributes. In this article, we'll first see how this property works, and how we can use it. Next, we'll see how it is not sufficient for most cases, and introduce a new approach, using inheritance.

BrowsableAttributes: the easy but insufficient way

With the PropertyGrid.BrowsableAttributes, you can choose to display only the properties that have the attributes contained in AttributeCollection. Suppose you want to only show the properties that are grouped in the "Appearance" category.

// You first create the attribute
// you want to filter with :
Attribute myfilterattribute = 
     new CategoryAttribute("Appearance");
// And you pass it to the PropertyGrid,
// via its BrowsableAttributes property :
mypropertygrid.BrowsableAttributes = new 
   AttributeCollection(new Attribute[] 
   { myfilterattribute });

You can choose several attributes, and of any sort:

mypropertygrid.BrowsableAttributes = 
  new AttributeCollection(new Attribute[] {
    new CategoryAttribute("Appearance"),
    new CategoryAttribute("Layout"),
    new DisplayNameAttribute("zzzz"),
    new BrowsableAttribute(false)

Note that by default, all the browsable attributes are displayed. In other words, if you let the BrowsableAttributes empty, you'll display all the properties of the SelectedObject that have the BrowsableAttribute set to true.

This is simple and works fine. But how can we just filter by properties? Unfortunately, the PropertyGrid doesn't provide a BrowsableProperties. If you want to display a selection of properties, you have to write some code ... or read the paragraph below.

Inheritance for selected properties: the FilteredPropertyGrid

Now, we'll discuss about another problem. Suppose your SelectedObject is a TextBox, and you want to display in a PropertyGrid only the "Size" property of your TextBox. As the PropertyGrid control is, it is not possible. If you read what is written in the previous section, you may imagine passing to the PropertyGrid.BrowsableAttributes property a collection of attributes like "PropertyNameAttribute". But unfortunately, such attributes don't exist.

I propose another approach: using a wrapper object that contains the object you want to display in the PropertyGrid. This wrapper will be passed to the PropertyGrid, instead of the object itself. So, the PropertyGrid will not directly display the object's properties, but the wrapper's ones. In this case, you need to subclass the PropertyGrid with a new control that overrides the SelectedObject property. That's exactly what I did, while writing the FilteredPropertyGrid control. This control is inherited from PropertyGrid and works the same way. I just added these useful properties:

  • BrowsableAttributes: overridden from PropertyGrid.
  • HiddenAttributes: all the properties that have the attributes contained in the collection are hidden.
  • BrowsableProperties: all the properties contained in the collection are displayed.
  • HiddenProperties: all the properties contained in the collection are not displayed.

Here's how you can use it. To display only the "Size" property of your TextBox, write these lines of code:

// Create the TextBox.
TextBox mycontrol = new TextBox();
// Create the inherited PropertyGrid.
FilteredPropertyGrid mygrid = new FilteredPropertyGrid();
// Set the only property visible.
mygrid.BrowsableProperties = new string[] { "Size" };
// Attach the control to the PropertyGrid.
mygrid.SelectedObject = mycontrol;
// Force the PropertyGrid to redraw itself

That's all! You can combine these four properties as you wish. Suppose you want to display all the categories, except the "Accessibility" and "Layout" ones. But you want the "AccessibilityRole" property, and not the "DataBinidngs" one. Write this code:

mygrid.HiddenPAttributes = 
   new AttibuteCollection(new Attribute[] {
       new CategoryAttribute("Accessibility"),
       new CategoryAttribute("Layout") });
mygrid.HiddenProperties = new string[] { "DataBinidngs" };
mygrid.BrowsableProperties = 
   new string[] { "AccessibilityRole" }

Note: these properties only work with SelectedObject. There is no warranty of them working with SelectedObjects.

How does it work?

Well, I will just describe the big pants of the FilteredPropertyGrid. For more information, you can download the source code above. When one of these four properties changes, the private method FilteredPropertyGrid.RefreshProperties() is called and builds a list containing all the PropertyDescriptors of the properties to display. This collection is passed to a wrapper, and the wrapper is linked to the PropertyGrid:

public class FilteredPropertyGrid : PropertyGrid
    private List<PropertyDescriptor> 
            m_PropertyDescriptors  = 
            new List<PropertyDescriptor>();
    private ObjectWrapper m_Wrapper = null;
    ( ... )

    public new object SelectedObject {
    get { return m_Wrapper != null ? 
          base.SelectedObject).SelectedObject : 
          null; }
        set {
            if(m_Wrapper == null) {
                m_Wrapper = new ObjectWrapper(value);
                m_Wrapper.PropertyDescriptors = 
            } else {
                // Link the wrapper to the parent PropertyGrid.
                base.SelectedObject = m_Wrapper;
    ( ... )

The RefreshProperties() method strongly uses the TypeDescriptor class and its GetProperties() method, to get the properties of the SelectedObject and build the list.


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

Software Developer
France France
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionFilterPropertGrid not opening SpreadSkin editor window For Farpoint Spread object While clicking on eclipse button of Skin propert Pin Pin
vikas@19907-Sep-14 19:20
membervikas@19907-Sep-14 19:20 
BugFilterPropertGrid not opening SpreadSkin editor window For Farpoint Spread object While clicking on eclipse button of Skin propert Pin
vikas@19904-Sep-14 6:13
membervikas@19904-Sep-14 6:13 
QuestionGreat job! Pin
zackp19838-Dec-11 12:20
memberzackp19838-Dec-11 12:20 
Question5 years over. Pin
RobbKirk14-Aug-11 2:50
memberRobbKirk14-Aug-11 2:50 
GeneralBug / Utilisation Pin
Eric Ouellet1-Apr-11 10:49
memberEric Ouellet1-Apr-11 10:49 
GeneralSetting only BrowsableProperties not work?! Pin
serhhio29-Apr-10 6:49
memberserhhio29-Apr-10 6:49 
GeneralReuse of code in commercial product Pin
Richard Bitango9-Feb-09 5:45
memberRichard Bitango9-Feb-09 5:45 
GeneralCode License Pin
eosjack16-Sep-08 16:20
membereosjack16-Sep-08 16:20 
Questionhow about SelectedObjects Pin
seanzcan12-May-08 16:46
memberseanzcan12-May-08 16:46 
AnswerRe: how about SelectedObjects Pin
Jake See12-Dec-12 19:57
memberJake See12-Dec-12 19:57 
GeneralRe: how about SelectedObjects Pin
Member 1008257718-Oct-13 2:28
memberMember 1008257718-Oct-13 2:28 
GeneralGood Reserch Pin
manas11-Dec-07 0:36
membermanas11-Dec-07 0:36 
GeneralSmall Bug fixed Pin
Hasson8127-Aug-07 6:31
memberHasson8127-Aug-07 6:31 
GeneralTabbing from Item To Item Pin
theMouse13-Jul-07 10:25
membertheMouse13-Jul-07 10:25 
QuestionDataBindings dropdown selection only shows "None" Pin
Peter Findt1-Jul-07 0:00
memberPeter Findt1-Jul-07 0:00 
GeneralDo not use BrowsableAttributes?! Pin
sahami18-Jun-07 11:41
membersahami18-Jun-07 11:41 
GeneralThank you Pin
ruben ruvalcaba8-May-07 14:59
memberruben ruvalcaba8-May-07 14:59 
QuestionWhat about expandable properties, dynamic components? Pin
wcoenen27-Feb-07 6:47
memberwcoenen27-Feb-07 6:47 
AnswerRe: Need a sample implementation of the same Pin
jackie_19846-Aug-07 19:43
memberjackie_19846-Aug-07 19:43 
GeneralRe: Need a sample implementation of the same Pin
connectpalm29-Feb-08 3:01
memberconnectpalm29-Feb-08 3:01 
GeneralGreat Article [modified] Pin
Todd Wilder28-Aug-06 18:38
memberTodd Wilder28-Aug-06 18:38 
Questiondisplay property in PropertyGrid Pin
poonam_iitr25-Jul-06 13:13
memberpoonam_iitr25-Jul-06 13:13 
GeneralICustomTypeDescriptor Pin
visualhint2-May-06 6:24
membervisualhint2-May-06 6:24 
GeneralRe: ICustomTypeDescriptor Pin
philippe dykmans8-Jun-08 6:47
memberphilippe dykmans8-Jun-08 6:47 
GeneralRe: ICustomTypeDescriptor Pin
visualhint8-Jun-08 7:13
membervisualhint8-Jun-08 7:13 
GeneralRe: ICustomTypeDescriptor Pin
philippe dykmans9-Jun-08 8:40
memberphilippe dykmans9-Jun-08 8:40 
GeneralRe: ICustomTypeDescriptor Pin
visualhint9-Jun-08 9:08
membervisualhint9-Jun-08 9:08 
GeneralCan't get more than one category to display! Pin
saidrad00731-Mar-06 21:52
membersaidrad00731-Mar-06 21:52 
GeneralRe: Can't get more than one category to display! Pin
suamikim4-Apr-06 23:39
membersuamikim4-Apr-06 23:39 
Generalproblem with your demo Pin
nexo5470025-Mar-06 11:24
membernexo5470025-Mar-06 11:24 
GeneralRe: problem with your demo Pin
bsargos26-Mar-06 10:00
memberbsargos26-Mar-06 10:00 
QuestionRe: problem with your demo Pin
nexo5470028-Mar-06 22:22
membernexo5470028-Mar-06 22:22 
AnswerRe: problem with your demo Pin
JoeSimons30-Jun-06 12:21
memberJoeSimons30-Jun-06 12:21 
GeneralRe: problem with your demo Pin
parisvb7-Apr-06 6:32
memberparisvb7-Apr-06 6:32 
GeneralThank you Pin
tonyt7-Mar-06 14:46
membertonyt7-Mar-06 14:46 
GeneralRe: Thank you Pin
bsargos10-Mar-06 0:30
memberbsargos10-Mar-06 0:30 
GeneralRe: Thank you Pin
tonyt27-Mar-06 23:23
membertonyt27-Mar-06 23:23 
GeneralRe: Thank you Pin
bsargos28-Mar-06 0:22
memberbsargos28-Mar-06 0:22 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151126.1 | Last Updated 27 Mar 2006
Article Copyright 2006 by bsargos
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid