Click here to Skip to main content
15,867,704 members
Articles / Programming Languages / C#

Exploring the .NET PropertyGrid in depth

Rate me:
Please Sign up or sign in to vote.
2.11/5 (5 votes)
26 Jun 2007CPOL2 min read 31.9K   429   13   1
Expolring the flexibilities that PropertyGrid offers to developers.

Introduction

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.

Background

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.

C#
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:

C#
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:

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

[Editor(typeof(Customer),typeof(UITypeEditor))]
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.

C#
[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

C#
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptionContext context)
{
    return UITypeEditorEditStyle.DropDown
}

The above method sets the UITypeEditor to a drop down.

C#
public override object EditValue(ITypeDescriptorContext context, 
                IServiceProvider provider, object value)
{
    dropdownlist.Items.Clear();
    dropdownlist.Items.AddRange(dropdownList);
    dropdownlist.Height = dropdownlist.PreferredHeight; 
    //Uses the IWindowsFormsEditorService to 
    //display a drop-down UI in the Properties 
    //window.
    edService = (IWindowsFormsEditorService)provider.GetService(
                       typeof(IWindowsFormsEditorService));
    if (edService != null)
    {
        edService.DropDownControl(dropdownlist);
        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.

License

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


Written By
Web Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralIntegrate with Enterprise Library Validation Application Block Pin
himanshu31323-Jul-09 11:09
himanshu31323-Jul-09 11: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.