Click here to Skip to main content
13,300,625 members (55,813 online)
Click here to Skip to main content
Add your own
alternative version


12 bookmarked
Posted 26 Jun 2007

Exploring the .NET PropertyGrid in depth

, 26 Jun 2007
Rate this:
Please Sign up or sign in to vote.
Expolring the flexibilities that PropertyGrid offers to developers.


This article helps in understanding the fundamentals of the .NET PropertyGrid. Developers who don't have first hand knowledge about the PropertyGrid can use this article to get a basic idea of how and where a PropertyGrid can be used.


PropertyGrid can be primarily used for displaying properties of a business object. It has flexibilities like being able to add dropdownlists and boolean properties to the grid, and hence exposes different types of operations that the end-user can perform on the grid.

Using the Code

The demo contains a form containing a PropertyGrid displaying the properties of a Customer class. The Customer class has properties like customer ID, type etc... I have inherited the UITypeEditor class from System.Drawing.Design. This class can be used to populate other types of controls, mainly drop down lists, in a PropertyGrid.

using System.Drawing.Design;
using System.ComponentModel;
using System.Windows.Forms;
using System.Windows.Forms.Design;

public class Customer : UITypeEditor

Declare a ListBox control and a Windows editor service to control the functionalities of the PropertyGrid:

ListBox dropdownlist = new ListBox();

[Display Name("Customer ID")]
public int CustomerID
    get{return m_CustomerID;}
    set{m_CustomerID = value;}

The end user will not be able to type string values in the customer ID field. The PropertyGrid will then throw this exception: "Property value Invalid". Isn't that cool??? On the other hand, the negative aspect of this validation is that this is a system thrown exception. So it is not advisable to override the exception to display a custom message as this might cause malfunctioning of the .NET PropertyGrid itself.

Let us now explore the other possibilities of using the PropertyGrid.

To display a property as a dropdown list, we need to use the UITypeEditor class. Consider the following example:

public static string[] dropdownlist;
public string[] typeList = {"Overseas","Localized",""}

public string CustomerType
    get{dropdownlist = typeList;}
    set{m_CustomerType = value;}return value;

On setting the Editor attribute to a particular property, the display style automatically changes at runtime. Now, let's see some other useful attributes that can be commonly used for a grid.

[CategoryAttribute("Customer Details"),Browsable(false)]
public int CustomerID
    get{return m_CustomerID;}
    set{m_CustomerID = value;}

The Category attribute can be used for grouping attributes under sections in the grid. The customer ID is a field that needs to be read only; to achieve this, the Browsable attribute can be used, which will grey out the field, preventing the end user from manipulating the value.

Methods in UITypeEditor

public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptionContext context)
    return UITypeEditorEditStyle.DropDown

The above method sets the UITypeEditor to a drop down.

public override object EditValue(ITypeDescriptorContext context, 
                IServiceProvider provider, object value)
    dropdownlist.Height = dropdownlist.PreferredHeight; 
    //Uses the IWindowsFormsEditorService to 
    //display a drop-down UI in the Properties 
    edService = (IWindowsFormsEditorService)provider.GetService(
    if (edService != null)
        return dropdownlist.SelectedItem;
    return value;

The above method assigns the specific string array to the dropdown list. The Windows Service helps to return the list through the UITypeEditor class.

Points of Interest

Manipulation of values in the PropertyGrid can be done at object level. This helps the developer to just pass the object instead of passing individual values for updating in the database, saving, and other database operations. For example, if you are using nHibernate to talk with a Firebird database, then the Customer object can be passed to the Update method of nHibernate.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
Germany Germany
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralIntegrate with Enterprise Library Validation Application Block Pin
himanshu31323-Jul-09 12:09
memberhimanshu31323-Jul-09 12:09 

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
Web02 | 2.8.171207.1 | Last Updated 26 Jun 2007
Article Copyright 2007 by keerti_maverick
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid