![]() |
Desktop Development »
Miscellaneous »
General
Intermediate
Exploring the .NET property grid in depthBy keerti_maverickExpolring the flexibilities that property grid offers to developers |
C# 2.0, Windows, .NET 2.0VS2005, Dev
|
||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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.
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
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
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.
| You must Sign In to use this message board. | ||||||||
|
||||||||
|
||||||||
|
||||||||
|
||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 26 Jun 2007 Editor: |
Copyright 2007 by keerti_maverick Everything else Copyright © CodeProject, 1999-2009 Web17 | Advertise on the Code Project |