Click here to Skip to main content
Click here to Skip to main content

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.

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.

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",""}

[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.

[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.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)

Share

About the Author

keerti_maverick
Web Developer
Germany Germany
No Biography provided

Comments and Discussions

 
GeneralIntegrate with Enterprise Library Validation Application Block Pinmemberhimanshu31323-Jul-09 11:09 

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 | Mobile
Web01 | 2.8.140826.1 | Last Updated 26 Jun 2007
Article Copyright 2007 by keerti_maverick
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid