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

Using the DataGridHelper Control with Custom Columns

[Download Code]

The DataGridHelper.aspx page demonstrates the use of DataGridHelper control with custom columns. A live demo can be accessed at http://www.dotneteer.com/projects/DataGridHelper/v3/DataGridHelperTest.aspx.

The custom columns are created and added to the DataGrid in the page_event as shown below:

private void DataGridHelperTest_Init(object sender, System.EventArgs e)
{
 DropDownColumn ddcSupplier = new DropDownColumn();
 ddcSupplier.DataField = "CompanyName";
 ddcSupplier.DataValueField = "SupplierID";
 ddcSupplier.HeaderText = "Supplier";
 ddcSupplier.SortExpression = "CompanyName";
 ddcSupplier.LoadDropDownList +=new LoadDropDownListEventHandler(ddcSupplier_LoadDropDownList);
 dataGridProducts.Columns.AddAt(4,ddcSupplier); 

 DropDownColumn ddcCategory = new DropDownColumn();
 ddcCategory.DataField = "CategoryName";
 ddcCategory.DataValueField = "CategoryID";
 ddcCategory.HeaderText = "Category";
 ddcCategory.SortExpression = "CategoryName";
 ddcCategory.LoadDropDownList +=new LoadDropDownListEventHandler(ddcCategory_LoadDropDownList);
 dataGridProducts.Columns.AddAt(5,ddcCategory); 

 CheckBoxColumn cbcDiscontinued = new CheckBoxColumn();
 cbcDiscontinued.DataField = "Discontinued";
 cbcDiscontinued.HeaderText = "Discontinued";
 cbcDiscontinued.SortExpression = "Discontinued";
 dataGridProducts.Columns.AddAt(11,cbcDiscontinued);
}

Note that it is possible to add the custom columns declaratively using tags. However, that would break the design-time behavior of the DataGrid control because it does not recognize the custom columns for the two DropDownColumn classes. Note that we also attached event handlers to the LoadDropDownList event of the DropDownColumn class. Since the two event handlers are nearly identical, we only show one below:

private void ddcCategory_LoadDropDownList(object Sender, LoadDropDownListEventArgs e)
{
 DropDownList ddl = e.DropDownList;
 if (sqlConnection.State != ConnectionState.Open)
  sqlConnection.Open();

 SqlCommand cmd = new SqlCommand("select CategoryID, CategoryName from Categories",sqlConnection);
 SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
 ddl.DataTextField = "CategoryName";
 ddl.DataValueField = "CategoryID";
 ddl.DataSource = dr;
 ddl.DataBind();
}

In the next section, we conclude this article.


View Entire Article

User Comments

Title: how to insert   
Name: eyal
Date: 2005-06-09 4:02:12 AM
Comment:
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
thenks
Title: Building a DataGrid Helper Control for ASP.NET 1.x: Part 3   
Name: greg
Date: 2005-04-30 12:07:57 PM
Comment:
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 asp.net 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
Comment:
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
Comment:
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
Comment:
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
Comment:
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
Comment:
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
Comment:
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
Comment:
Sam,

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

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

Product Spotlight
Product Spotlight 





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


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-29 10:01:23 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search