Click here to Skip to main content
12,953,223 members (45,228 online)
Click here to Skip to main content
Add your own
alternative version


23 bookmarked
Posted 7 Mar 2006

Create Template column dynamically in DataGrid

, 7 Mar 2006
Rate this:
Please Sign up or sign in to vote.
This articles helps in creating template columns dynamically with bound columns using a template class

Sample Image - DynamicTemplateColumnExample.jpg


This Article helps in creation of template columns dynamically . I got a condition when i have to display few column values from database and few template (checkbox) column dynamically.

I have created a sample template class which is required in project.Firstly modify template class as per your requirement.As here i have given checkbox as template column example.

Steps to implement code in your project :

1) Set the DataGrid AutoGenerateColumns property to false .

2) Example to Bound the static Columns

<asp:DataGrid id="ItemsGrid" runat="server" OnItemCommand="Grid_CartCommand" AutoGenerateColumns="False"
CellPadding="3" BorderWidth="1px" BorderColor="Black">
<HeaderStyle BackColor="#C0C0FF"></HeaderStyle>
<asp:ButtonColumn Text="Add" ButtonType="PushButton" HeaderText="Add to cart" CommandName="AddToCart"></asp:ButtonColumn>
<asp:ButtonColumn Text="Remove" ButtonType="PushButton" HeaderText="Remove from cart" CommandName="RemoveFromCart"></asp:ButtonColumn>
<asp:BoundColumn DataField="StringValue" HeaderText="Item"></asp:BoundColumn>
<asp:BoundColumn DataField="CurrencyValue" HeaderText="Price" DataFormatString="{0:c}">
<ItemStyle HorizontalAlign="Right"></ItemStyle>

3) In Page_load add following Code

if (!IsPostBack)
// Load this data only once.
ItemsGrid.DataSource= CreateDataSource(); //Function to create dynamic column

<headERSTYLE BackColor="#C0C0FF"></headERSTYLE><columns /><asp:ButtonColumn CommandName="AddToCart" HeaderText="Add to cart" ButtonType="PushButton" Text="Add"></asp:ButtonColumn><asp:ButtonColumn CommandName="RemoveFromCart" HeaderText="Remove from cart" ButtonType="PushButton" Text="Remove"></asp:ButtonColumn><asp:BoundColumn HeaderText="Item" DataField="StringValue"></asp:BoundColumn><asp:BoundColumn HeaderText="Price" DataField="CurrencyValue" DataFormatString="{0:c}">

4) In OnInit() add following code protected void OnInit(EventArgs e)// // CODEGEN: This call is required by the ASP.NET Web Form Designer. //










void CreateDataGridColumn()// Create dynamic column to add to Columns collection. //-----------------------------------------------------------------------------

TemplateColumn tc1 =

//-----Class used named DataGridTempla.cs------

tc1.HeaderTemplate =

tc1.ItemTemplate =

tc1.EditItemTemplate =

tc1.FooterTemplate =


new TemplateColumn();new DataGridTempla(ListItemType.Header, "Select1");new DataGridTempla(ListItemType.Item, "Select1");new DataGridTempla(ListItemType.EditItem, "");new DataGridTempla(ListItemType.Footer, "");//-----------------------------------------------------------------------------

TemplateColumn tc2=




new TemplateColumn();new DataGridTempla(ListItemType.Header,"Select2");new DataGridTempla(ListItemType.Item,"Select2");//-----------------------------------------------------------------------------

BoundColumn NumberColumn =

NumberColumn.HeaderText="Item Number";


new BoundColumn();// Add column to Columns collection.

ItemsGrid.Columns.AddAt(2, NumberColumn);


<itemstyle horizontalalign="Right" /></itemstyle /></asp:BoundColumn></columns />



This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Dolly Jain
Web Developer
India India
Be helpful.When you see a person without a smile ,Give him yours........

(Zig Ziglar)

You may also be interested in...

Comments and Discussions

Questiongot error Pin
phoohtoo23-Aug-15 17:26
memberphoohtoo23-Aug-15 17:26 
QuestionSample discuss Pin
Miguel Nazario5-Mar-08 7:12
memberMiguel Nazario5-Mar-08 7:12 
GeneralGr8 Job Done Pin
Preeti Agrawal13-Dec-07 19:56
sussPreeti Agrawal13-Dec-07 19:56 
GeneralEvents lost: do it Before OnInit Pin
Ricardo Casquete14-Mar-06 0:27
memberRicardo Casquete14-Mar-06 0:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170525.1 | Last Updated 8 Mar 2006
Article Copyright 2006 by Dolly Jain
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid