Click here to Skip to main content
11,630,646 members (78,146 online)
Click here to Skip to main content

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

, 11 Jan 2005 137.8K 3.4K 97
Rate this:
Please Sign up or sign in to vote.
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.

License

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

Share

About the Author

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

He blogs at http://blog.sreesharp.com

He maintains the following sites.

http://www.industrial-automation-software.com
http://forum.t-mug.org
http://www.t-mug.org
http://www.sreesharp.com


You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
Tarek.Eladly13-Feb-13 0:36
memberTarek.Eladly13-Feb-13 0:36 
GeneralMy vote of 5 Pin
Thorsten Bruning21-Dec-12 1:32
memberThorsten Bruning21-Dec-12 1:32 
GeneralMy vote of 5 Pin
oozakala4-Dec-12 6:53
memberoozakala4-Dec-12 6:53 
GeneralMy vote of 5 Pin
Volynsky Alex7-Jun-12 5:41
memberVolynsky Alex7-Jun-12 5:41 
GeneralLabview can use PropertyGrid thanks to your code Pin
pcncottin30-Jul-08 18:11
memberpcncottin30-Jul-08 18:11 
Hello,
Your piece of code just saved my life! I have been trying for several days to make the PropertyGrid control work in LabView. LabView can interact with .NET objects and controls but does not have the ability to create a .NET object required to populate a PropertyGrid. Thanks to your new class, it's not a problem anymore. I will be able to display an arbitrary cluster of data (structure) in a PropertyGrid at runtime. I have not tried to change the attribute yet but it should work as well.
Thanks again, good job

Pierre Cottin
QuestionDatatype sensitve properties Pin
harish_somanchi22-Mar-07 10:05
memberharish_somanchi22-Mar-07 10:05 
GeneralThere is a small bug inside the code Pin
alp ates26-Dec-06 4:04
memberalp ates26-Dec-06 4:04 
GeneralType Pin
Lions_italy16-Jan-06 21:28
memberLions_italy16-Jan-06 21:28 
QuestionIs it possible to change [Readonly( )] attribute at runtime? Pin
jjohn20056-Dec-05 22:13
memberjjohn20056-Dec-05 22:13 
AnswerRe: Is it possible to change [Readonly( )] attribute at runtime? Pin
Locusta7411-Feb-07 18:40
memberLocusta7411-Feb-07 18:40 
GeneralLook a little further Pin
ByteGhost28-Feb-05 11:38
memberByteGhost28-Feb-05 11:38 
GeneralDynamic list property Pin
Poletto30-Jan-05 22:23
memberPoletto30-Jan-05 22:23 
GeneralRe: Dynamic list property Pin
salle7824-Nov-08 2:27
membersalle7824-Nov-08 2:27 
GeneralAttributes Pin
damslang24-Jan-05 22:25
memberdamslang24-Jan-05 22:25 
GeneralRe: Attributes Pin
damslang24-Jan-05 23:30
memberdamslang24-Jan-05 23:30 
GeneralExtending dynamic propertygrid capabilities Pin
Poletto24-Jan-05 21:56
memberPoletto24-Jan-05 21:56 
GeneralRe: Extending dynamic propertygrid capabilities Pin
varad273-Sep-07 21:20
membervarad273-Sep-07 21:20 
GeneralRe: Extending dynamic propertygrid capabilities Pin
Clayton Hotson18-Mar-10 5:54
memberClayton Hotson18-Mar-10 5: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
Web02 | 2.8.150723.1 | Last Updated 12 Jan 2005
Article Copyright 2005 by Sreejumon
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid