Dynamic GridView and DataList in ASP.NET 2.0
page 8 of 10
by Satheesh Babu
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 36376/ 791

Using the Template Class in GridView

Using dynamic template in GridView is slightly different from DataList. We will create the dynamic GridView in column wise with header template, item template and footer template from the first column to the last.

Steps

1.    Create a GridView Object.

2.    Create an instance of TemplateField object.

3.    Instantiate the Dynamic template class with proper ListItemType and assign it to the corresponding template property of TemplateField object and finally add this object to the column collection of GridView.

Listing 6: Templates of GridView

TemplateField tf = new TemplateField();
tf.HeaderTemplate = new DynamicGridViewTextTemplate("ArticleID",
 DataControlRowType.Header);
tf.ItemTemplate = new DynamicGridViewTextTemplate("ArticleID",
 DataControlRowType.DataRow);
tf.FooterTemplate = new DynamicGridViewTextTemplate(DataControlRowType.Footer,
 ds.Tables[i].Rows.Count);             

If you compare the implementation of DataList, in Gridview we will not create a dynamic template for the grid, instead we create it for the grid’s column (TemplateField).

Listing 7: Using Template class

for (int i = 0; i < ds.Tables.Count; i++)
        {
            if (ds.Tables[i].Rows.Count > 0)
            {
                GridView gvDynamicArticle = new GridView();
                gvDynamicArticle.Width = Unit.Pixel(700);
                gvDynamicArticle.BorderWidth = Unit.Pixel(0);
                gvDynamicArticle.Caption = 
                   "<div id=\"nifty\" class=\"PostCategory\">" + 
                   ds.Tables[i].Rows[0]["Category"].ToString() + 
                   " Articles</div>";
                gvDynamicArticle.AutoGenerateColumns = false;
                gvDynamicArticle.ShowFooter = true;
                TemplateField tf = null;
 
                tf = new TemplateField();
                tf.HeaderTemplate = new DynamicGridViewTextTemplate("ArticleID", 
                   DataControlRowType.Header);
                tf.ItemTemplate = new DynamicGridViewTextTemplate("ArticleID", 
                   DataControlRowType.DataRow);
                tf.FooterTemplate = new DynamicGridViewTextTemplate(
                   DataControlRowType.Footer, ds.Tables[i].Rows.Count);                
               
                gvDynamicArticle.Columns.Add(tf);
 
                tf = new TemplateField();
                tf.HeaderTemplate = new DynamicGridViewTextTemplate("Title", 
                   DataControlRowType.Header);
                tf.ItemTemplate = new DynamicGridViewTextTemplate("Title", 
                   DataControlRowType.DataRow);
                gvDynamicArticle.Columns.Add(tf);
 
                tf = new TemplateField();
                tf.HeaderTemplate = new DynamicGridViewTextTemplate("Description", 
                   DataControlRowType.Header);
                tf.ItemTemplate = new DynamicGridViewTextTemplate("Description", 
                   DataControlRowType.DataRow);
                gvDynamicArticle.Columns.Add(tf);
 
                tf = new TemplateField();
                tf.HeaderTemplate = new DynamicGridViewURLTemplate("Title""URL", 
                   DataControlRowType.Header);
                tf.ItemTemplate = new DynamicGridViewURLTemplate("Title""URL", 
                   DataControlRowType.DataRow);
                gvDynamicArticle.Columns.Add(tf);
 
                tf = new TemplateField();
                tf.HeaderTemplate = new DynamicGridViewTextTemplate("Author", 
                   DataControlRowType.Header);
                tf.ItemTemplate = new DynamicGridViewTextTemplate("CreatedBy", 
                   DataControlRowType.DataRow);
                gvDynamicArticle.Columns.Add(tf);
 
 
                gvDynamicArticle.RowDataBound += new 
                   GridViewRowEventHandler(this.DynamicGrid_RowDataBound);
 
                gvDynamicArticle.DataSource = ds.Tables[i];
                gvDynamicArticle.DataBind();
                phDynamicGridHolder.Controls.Add(gvDynamicArticle);
            }
        }

In the above code (Listing 6 –Using Template class) we can clearly understand that we are creating the dynamic template for the GridView's column as opposed to DataList where we created the template for the grid itself. To throw more light on this it means that we are creating the first column’s header, item and footer and adding it to the GridView's column list through TemplateField object to the last column as I said earlier.


View Entire Article

Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 3 and 3 and type the answer here:

User Comments

Title: JSON LINQ   
Name: willian brasil
Date: 10/3/2008 1:18:03 PM
Comment:
Very Good!
Title: Data List   
Name: Deleep Kumar Pavana
Date: 9/11/2008 5:03:07 AM
Comment:
Hi satheesh,
Its good but repeat colomns property is not working properly on that,if i use table tag to display data in table format..
Title: Good Work ....   
Name: Mudassar C
Date: 7/17/2008 1:09:10 AM
Comment:
Hi Sateesh,
Its a really good article to read..
Good piece of Code..

Mudassar Chandle
Title: Data List And GridView   
Name: Santosh Kumar
Date: 6/24/2008 3:36:54 AM
Comment:
Hi Satheesh,
I read this article , good work done.
Santosh Kumar
http://www.operativesystems.com
Title: VB Please   
Name: Matt
Date: 6/3/2008 1:11:24 AM
Comment:
Any chance I can get this in VB?
Please
THanks
Matt
Title: problem with binding   
Name: Kannan
Date: 5/15/2008 2:51:40 AM
Comment:
All item template values displayed at once.no reapeated datalist display
Title: muhajiran bugok   
Name: muhajiran bugok
Date: 5/9/2008 6:43:36 AM
Comment:
this is very helpful..
-putang ina nyo lahat
Title: sivaram   
Name: sivaram
Date: 2/4/2008 1:47:59 AM
Comment:
Thanks.

hi i read your article.it is well and good article.

Product Spotlight
Product Spotlight 
Learn More
.NET Tools
asp.net shopping cart
asp.net chart control






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


©Copyright 1998-2008 ASPAlliance.com  |  Page Processed at 12/3/2008 7:11:35 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search