5,702,921 members and growing! (14,721 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » General     Intermediate

Exploring the .NET property grid in depth

By keerti_maverick

Expolring the flexibilities that property grid offers to developers
C# 2.0, C#, Windows, .NET, .NET 2.0VS2005, Visual Studio, Dev

Posted: 26 Jun 2007
Updated: 26 Jun 2007
Views: 7,555
Bookmarked: 5 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
4 votes for this Article.
Popularity: 1.05 Rating: 1.75 out of 5
2 votes, 50.0%
1
1 vote, 25.0%
2
1 vote, 25.0%
3
0 votes, 0.0%
4
0 votes, 0.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

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

Background

Property grid can be primarily used for displaying properties of a buisness object. This contains flexibilities like adding dropdownlists , boolean properties to the grid and hence expose the different types of operations that the end-user can perform on a property grid

Using the code

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

//Using System.Drawing.Design;
//Using System.ComponentModel;
//Using System.Windows.Forms;
//Using System.Windows.Forms.Design;

//public class Customer : UITypeEditorDeclare a ListBox Control and a windows editor service to control the functionalities of the Property grid

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 Property grid will throw an exception as "Property value Invalid".Aint this 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 ovveride the exception to display a custom message as this might cause malfunctioning of the .NET Property Grid itself.

Let us now explore the other possibilities of the property grid

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 on runtime.Now lets 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 the attributes under sections in the grid.The Customer ID is a field that needs to be read only , to acheive 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 dropdownlist. The Windows service helps to return the list through the UIType Editor class

Points of Interest

Manipulation of values in the property grid can be done at the object level. This helps the developer to just pass the object instead of passing individual values for updation in database , saving and other db operations.For example if you using NHibernate to talk with a Firebird database then the customer object can be passed to the update method of NHibernate.

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

keerti_maverick



Occupation: Web Developer
Location: Germany Germany

Other popular Miscellaneous 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   
  (Refresh) 
-- There are no messages in this forum --

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

PermaLink | Privacy | Terms of Use
Last Updated: 26 Jun 2007
Editor:
Copyright 2007 by keerti_maverick
Everything else Copyright © CodeProject, 1999-2008
Web15 | Advertise on the Code Project