Building a DataGrid Helper Control for ASP.NET 1.x: Part 3
page 3 of 6
by Li Chen
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 32548/ 50

Creating Custom DataGrid Columns

[Download Code]

We included two custom DataGrid columns in this article: the CheckBoxColumn and the DropDownColumn. The custom DataGrid columns included in this article were created in a similar way to the custom columns on the MetaBuilders site and in Marcie Robillard’ MSDN article. We encourage you to read Marcie Robillard’s MSDN article if you are interested in creating custom columns. In this article, we will focus on the feature and the difference in our implementation.

The CheckBoxColumn is a very simple class and has only two properties:

DataField The data field to bind to.
ReadOnly The CheckBoxColumn always render data as CheckBox. The CheckBox is only enabled when the item is an edit item and the ReadOnly property is equal to false.

The design of DropDownColumn is somewhat different to the design of Marcie Robillard and MetaBuilders. Both the Marcie Robillard version and the MetaBuilders version expose a DataSource property for users to populate the data source. Since DataSource is not the only way to populate a DropDownList control, we decide to expose the underlying DropDownList control instead so that users can populate it in any way of their choice. The other important difference is the optimization we made for the population of the DropDownList. Since the DropDownList is used only in edit mode, we should not populate the DropDownList until we need to. For this reason, DropDownColumn exposes the LoadDropDownList event in which we can populate the DropDownList. If we care more about the simplicity than optimization, we could still populate the DropDownList in the Page_Int or Page_Load event using the DropDownList property of the DropDownColumn class. The following is the complete list of DropDownColumn properties:

DataField  The data field to be displayed. It is also the data field used for to populate the name/value pair used for update if the DataValueField is not supplied. This field is required.
DataFormatString  Used to format the data field displayed.
DataValueField  If supplied, the DataValueField and selected value of the DropDownList would be used to form the name/value pair for update.
DropDownList Used to access the underlying DropDownList. Note that for optimization purpose, the actually DropDownList object is only instantiated when it is necessary to instantiate the DropDownList, or this property is accessed, whichever occurs first.
ReadOnly  If the value of this property is true, the DropDownList is disabled.

Let’s first take a look of the implement the IDataGridColumn interface in the CheckBoxColumn:

public void GetValues(TableCell cell, Hashtable col)
 bool val = ((CheckBox)cell.Controls[0]).Checked;
 col[_dataField] = val;

We simply extract the value of the CheckBox control.

Now let us take a look of the implement the IDataGridColumn interface in the DropDownColumn:

public void GetValues(TableCell cell, Hashtable col)
 string name;
 object val;
 if  (this.DataValueField == null || this.DataValueField == "")
  name = this.DataField;
  val = _ddl.SelectedItem.Text;
  name = this.DataValueField;
  val = _ddl.SelectedItem.Value;
 col[name] = val;

Note that we extract the text or value of the SelectedItem depending on whether the DataValueField property is set.

Next, we will discuss how to use an arbitrary custom DataGrid column with the DataGridHelper control.

View Entire Article

User Comments

Title: how to insert   
Name: eyal
Date: 2005-06-09 4:02:12 AM
i wont to insert to grid in a way that the colomn will open in a visual stae in the grid mach like the edit command if you can help me
Title: Building a DataGrid Helper Control for ASP.NET 1.x: Part 3   
Name: greg
Date: 2005-04-30 12:07:57 PM
he control is very cool.
I have a question regarding the DataGridHelper control:
1. why do we need a DataGridHelperconnector component? the web component is disappeared in 2.0

2. can we set the properties dataAdapter, datagrid to browsable instead of using the connector? so that we can set these properties at design time.

I'm lokking forward to your answer, Thanks
Title: Re: Small bug   
Name: Li Chen
Date: 2004-12-22 12:12:44 PM
I would like to thank Robert for pointing out the bug in part 2 of the article. The bug was discovered during the writing of the part 3 of this series and fixed. Readers are urged to use the code from part 3.
Title: Small bug   
Name: Robert Storrs
Date: 2004-12-01 2:34:39 PM
Great Article. One of the best I have read in some time.

I did find a small bug and added the following line as line 134 in the DataGridHelperConnector.cs (in version 2)

134 if(_dataGridHelper.DataGrid != _dataGrid)
_dataGridHelper.DataGrid = _dataGrid;

Without this check, the program creates two event handlers for the SortCommand. This results in the new sort order always being DESC. It also caused multiple reloading of the grid.
Title: Everybody speaks C#   
Name: Shain
Date: 2004-09-14 9:06:50 PM
Any computer science program should know the basics of languages similiar to Java, C++, C, or the like. It is not very hard to convert C# code to VB code and since this article really explores Data Structures & Algorithms approaches it is pretty easy to convert. You should study this article, and if you need VB, try to write the code yourself. There is probably an advantage to having this as C# code -- although the CLR may not care.
Title: love it   
Name: ben
Date: 2004-09-14 7:09:11 AM
love it
Title: Building a DataGrid Helper Control for ASP.NET 1.x: Part 3   
Name: Peter Ho
Date: 2004-09-12 12:29:05 AM
A great series of articles, Mr. Chen. Thanks!
Title: Building a DataGrid Helper Control for ASP.NET 1.x: Part 3   
Name: T. freeman
Date: 2004-08-16 11:07:09 AM
Excellent article - well written, good detail. Thanks!
Title: Building a DataGrid Helper Control for ASP.NET 1.x: Part 3   
Name: Li Chen
Date: 2004-07-20 12:53:03 PM

The download does include the VB version of the DataGridHelperTest project.

Title: Building a DataGrid Helper Control for ASP.NET 1.x: Part 3   
Name: Sam Richardson
Date: 2004-07-20 9:27:49 AM
It would be even better if there was some code to go with it - not everyone speaks C#.

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-04-18 7:52:40 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search