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): 87123/ 200

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

User Comments

Title: aaaa   
Name: aaaa
Date: 2012-10-19 12:48:33 AM
Comment:
aaaaa
Title: 6u5u75   
Name: 657657
Date: 2012-10-19 12:48:18 AM
Comment:
hgfgfhfgh
Title: sgrryry   
Name: ryryytr
Date: 2012-08-18 9:00:37 AM
Comment:
hkhafsjkfbudssdjfwuifwfbweufwefwefbewjfuwiefw
Title: 2012 NFL jerseys   
Name: NIKE NFL jerseys
Date: 2012-05-20 11:33:16 PM
Comment:
[/pre]Cheap NFL,NBA,MLB,NHL
[url=http://www.jersey2shop.com/]Jerseys From China[/url]
[url=http://www.jersey2shop.com/]2012 nike nfl Jerseys[/url]
[url=http://www.jersey2shop.com/]cheap China Jerseys[/url]
[url=http://www.jersey2shop.com/]Sports Jerseys China[/url]
[url=http://www.jersey2shop.com/NFL-Jerseys-c68/]NFL Jerseys China[/url]
[url=http://www.jersey2shop.com/NBA-Jerseys-c77/]NBA Jerseys China[/url]
NHL Jerseys China
[url=http://www.jersey2shop.com/MLB-Jerseys-c94/]MLB Jerseys China[/url]NFL jerseys For Sale online.All Our Jerseys Are Sewn On and Directly From Chinese Jerseys Factory
[/pre]
[pre]We Are Professional China jerseys Wholesaler
[url=http://www.cheapjersey2store.com/]Wholesale cheap jerseys[/url]Cheap mlb jerseys
[url= http://www.cheapjersey2store.com/]2012 mlb all atar jerseys[/url]
[url= http://www.cheapjersey2store.com/ [/url]Cheap China Wholesael[/url]
[url= http://www.cheapjersey2store.com/]Wholesale jerseys From China[/url]
[url=http://www.cheapjersey2store.com/]2012 nike nfl Jerseys[/url]Free Shipping,Cheap Price,7 Days Deliver
[/pre]
[/pre]
We are professional jerseys manufacturer from china,wholesal
sports [url= http://www.cheapjersey2store.com/]Jerseys From China[/url]
[url=http://www.cheapjersey2store.com/NFL-Jerseys-c68]NFL jerseys China[/url]
[url=http://www.cheapjersey2store.com/NHL-Jerseys-c96/]NHL Jerseys China[/url]
[url=http://www.cheapjersey2store.com/NBA-Jerseys-c77/]NBA Jerseys China[/url]
[url=http://www.cheapjersey2store.com/MLB-Jerseys-c94/]MLB Jerseys China[/url]
[url= http://www.cheapjersey2store.com/]China Jerseys[/url],Free Shipping
[/pre]
[/pre]
We are professional jerseys manufacturer from china,wholesal
sports [url= http://www.jerseycaptain.com/]cheap jerseys sale online [/url]
[url= http://www.jerseycaptain.com/]2012 nike nfl Jerseys[/url]
[url=http://www.jerseycaptain.com/NFL-Jerseys-c68]cheap NFL jerseys China[/url]
[url=http://www.jerseycaptain.com/NHL-Jerseys-c96/]NHL Jerseys C
Title: good article   
Name: Mahmoud ALGoul
Date: 2011-02-02 5:51:30 AM
Comment:
cool article, but how I can add paging for the code, any help please.
Title: FANTASTIC!!   
Name: André
Date: 2010-03-12 5:02:57 PM
Comment:
Thank You!! Great job and easy to understand..
Title: web filtering policy   
Name: adel
Date: 2009-07-29 5:47:19 AM
Comment:
i need to design web filtering policy
Title: Itemplate funtion not working   
Name: Prabhat Sinha
Date: 2009-06-25 7:17:46 AM
Comment:
its really good work in C# but itemplete funtion not working in vb.net please help me
Title: If items dissapear in postback   
Name: TiagoReil
Date: 2009-06-16 1:23:08 PM
Comment:
I had to do something like this, and had lots of problems. First of all, when you do a postback, you loose all the dinamic templates. Not databounts added dinamically, but only the ones that are templates disapear always. So I run the code on every postback. But then the events in the grid, that are loaded dinamically, started acting strange. Runing events that had already run, etc.

The solution and correct way to do this is to add all the code for adding columns on the PreInit event. Thats the correct place to add the creation of the columns.

Santiago.
Title: Great example   
Name: Vijay
Date: 2009-06-06 3:36:28 AM
Comment:
What is DynamicGrid_RowDataBound doing?
Title: Working example   
Name: Mindaugas
Date: 2009-06-03 5:05:06 AM
Comment:
Working example. Thank you!
Title: Dynamic GridView   
Name: Shaju M.K
Date: 2009-02-12 5:38:02 PM
Comment:
Hi Satheesh Babu,
Really its a very nice article that helped me to solve the issue that I faced in the development stage.

Thank You Very much!
Title: JSON LINQ   
Name: willian brasil
Date: 2008-10-03 1:18:03 PM
Comment:
Very Good!
Title: Data List   
Name: Deleep Kumar Pavana
Date: 2008-09-11 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: 2008-07-17 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: 2008-06-24 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: 2008-06-03 1:11:24 AM
Comment:
Any chance I can get this in VB?
Please
THanks
Matt
Title: problem with binding   
Name: Kannan
Date: 2008-05-15 2:51:40 AM
Comment:
All item template values displayed at once.no reapeated datalist display
Title: muhajiran bugok   
Name: muhajiran bugok
Date: 2008-05-09 6:43:36 AM
Comment:
this is very helpful..
-putang ina nyo lahat
Title: sivaram   
Name: sivaram
Date: 2008-02-04 1:47:59 AM
Comment:
Thanks.

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

Product Spotlight
Product Spotlight 





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


©Copyright 1998-2021 ASPAlliance.com  |  Page Processed at 2021-03-01 5:03:21 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search