5,699,997 members and growing! (17,673 online)
Email Password   helpLost your password?
Desktop Development » Tabs & Property Pages » Tabs and Property Pages     Intermediate

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

By Sreejumon

Modify PropertyGrid control Items collection, at runtime.
C#Windows, .NET, .NET 1.1, .NET 2.0, Win2K, WinXP, Win2003VS.NET2003, VS2005, Visual Studio, Dev

Posted: 12 Jan 2005
Updated: 12 Jan 2005
Views: 62,104
Bookmarked: 61 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
30 votes for this Article.
Popularity: 6.46 Rating: 4.37 out of 5
2 votes, 6.7%
1
3 votes, 10.0%
2
1 vote, 3.3%
3
6 votes, 20.0%
4
18 votes, 60.0%
5

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

About the Author

Sreejumon


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


Occupation: Web Developer
Location: India India

Other popular Tabs & Property Pages articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 13 of 13 (Total in Forum: 13) (Refresh)FirstPrevNext
GeneralLabview can use PropertyGrid thanks to your codememberpcncottin19:11 30 Jul '08  
QuestionDatatype sensitve propertiesmemberharish_somanchi11:05 22 Mar '07  
GeneralThere is a small bug inside the codememberalp ates5:04 26 Dec '06  
GeneralTypememberLions_italy22:28 16 Jan '06  
GeneralIs it possible to change [Readonly( )] attribute at runtime?memberjjohn200523:13 6 Dec '05  
AnswerRe: Is it possible to change [Readonly( )] attribute at runtime?memberLocusta7419:40 11 Feb '07  
GeneralLook a little furthermemberByteGhost12:38 28 Feb '05  
GeneralDynamic list propertymemberPoletto23:23 30 Jan '05  
GeneralRe: Dynamic list propertymembersalle783:27 24 Nov '08  
GeneralAttributesmemberdamslang23:25 24 Jan '05  
GeneralRe: Attributesmemberdamslang0:30 25 Jan '05  
GeneralExtending dynamic propertygrid capabilitiesmemberPoletto22:56 24 Jan '05  
GeneralRe: Extending dynamic propertygrid capabilitiesmembervarad2722:20 3 Sep '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 12 Jan 2005
Editor: Smitha Vijayan
Copyright 2005 by Sreejumon
Everything else Copyright © CodeProject, 1999-2008
Web18 | Advertise on the Code Project