Dynamically Creating Bound and Template Columns in GridView
By AzamSharp
Views: 33888

 

Introduction:

There are situations when you need the columns to be created dynamically. Creating dynamic columns in GridView control is almost identical to Datagrid control. If you are working with Datagrid control than check out my article Creating Dynamic Bound and Template Columns in Datagrid Control. In this article we will see how to create dynamic bound and template columns for the GridView.

Dynamically Creating BoundColumn for GridView:

BoundColumns are pretty straight forward. Check out the code below:

BoundField nameColumn = new BoundField();

nameColumn.DataField = "Name";

nameColumn.HeaderText = "Person Name";

GridView1.Columns.Add(nameColumn);

All you do is make an object of the BoundField class. Assign the DataField, HeaderText and finally add it to the GridView columns collection. There must be any source from which we get the data field and in this case the source is SqlDataSource object. For more information about how to assign SqlDataSource to the GridView control please check out my GridView articles.

Dynamically Creating a Template Column:

Now let's see how we can create template columns dynamically. Since template columns can contains any control you must define a custom template which creates the controls that are identified. You can download the template file which is present in the zip file at the end of this article.

The code below shows how you can use the custom template "GridViewTemplate.cs".

TemplateField ckhColumn = new TemplateField();

ckhColumn.HeaderTemplate = new GridViewTemplate(ListItemType.Header, "CheckBox Column");

ckhColumn.ItemTemplate = new GridViewTemplate(ListItemType.Item, "some data");

GridView1.Columns.Add(ckhColumn);

First we make an instance of the TemplateField class. Than we set the HeaderTemplate type by passing the ListItemType.Header and the name of the template column to be created. Than we set the ItemTemplate and finally we add the newly created template column in the GridView columns collection.

Since I am using SqlDataSource object it will take care of binding for me but if you are using a different data source than you must call GridView1.DataBind() method to bind it on the screen.

Please view GridViewTemplate.cs file which contains the main code to create the template columns.   

I hope you liked the article, happy coding!

 

By AzamSharp


Enter Comment/Feedback
  •  
  •  
  •  
  •  
  •  

Comments/Feedbacks
Subject: Great
Name: Eduardo Cobuci
Date: 1/28/2007 12:00:53 PM
Comment:
Nice article Azam, and thank you!
Subject: hm..
Name: HyVong
Date: 2/2/2007 8:42:22 AM
Comment:
Your code is great but for some one as a beginer, they will have some difficultty. And most of beginner programmers are like to search for this info. it would be nicer if you include some sample where the actual should be placed in.
Thanks though :)
Subject: Need some help
Name: Razin
Date: 2/5/2007 3:56:53 AM
Comment:
I bind my data source to my GridView. After then I've added two button column. I need the button columns appear at the end of my data source. But it appears at column no 1 and 2. What's the prob. with my code. Can u plz give me some solution?

GridView_Section = new GridView();
GridView_Section.ID = "GridView1";

GridView_Section.DataSource = dTable;

HyperLinkField _hLinkField = new HyperLinkField();
_hLinkField.HeaderText = "Edit";
_hLinkField.Text = "Edit...";
_hLinkField.NavigateUrl="my.aspx";
GridView_Section.Columns.Add(_hLinkField);

ButtonField _btnField = new ButtonField();
_btnField.HeaderText = "Delete";
_btnField.ButtonType = ButtonType.Image; GridView_Section.Columns.Add(_btnField);

GridView_Section.DataBind();
Thanx.
Subject: Query
Name: Pradeep Kothiyal
Date: 2/20/2007 3:17:10 AM
Comment:
Nice article!!
Azam, I have created gridview dynamically using the template field.Everything goes fine.But I am unable to implement paging and sorting with this girdview.
As soon as I add Paging related properties to gridview,along with gridview event definition,it says " Object reference not set to an instance of an object.",when I try to do databinding with gridview.
I seek your valuable guidence.
Subject: Paging
Name: Murahari
Date: 4/6/2007 3:42:23 AM
Comment:
Hi Azam,
Good Article.
Need your guidence to implement paging in Gridview
I am also having the same problem that Pradeep Kothiyal mentioned here. My Gridview works fine if i bind the data using Sqldatasource control. But it is giving error when i bind the data using Sqldatasource dynamically.(i.e. page_load).

Thanks in advance.


Subject: RE: Paging
Name: AzamSharp
Date: 4/12/2007 1:14:09 PM
Comment:
Hi,

Make sure you are only binding when not postback!
Subject: hi
Name: sai
Date: 5/31/2007 10:20:46 PM
Comment:
hi this is sai you covered the topics very nicely . i learnt so much from u r articles thanqs for that.
you continue the thing for us
Subject: GridView TemplateField delete button
Name: Richie
Date: 6/6/2007 1:43:23 AM
Comment:
How to add a delete button to TemplateField to make it work correctly? I've add button to template field with commandName="Delete". When I click it pages postback and nothing happens. I set also CommandEventHandler to GridView but that event never fires. I do not know how to make it work. Please help.
Subject: Nice
Name: Super
Date: 6/6/2007 3:23:24 AM
Comment:
Hello,

i have used this gridview. It works great.

But how can i get the value from the textbox from the template field which is in the gridview?

Thanks again.
Subject: Button field template
Name: Hendrik
Date: 8/8/2007 3:32:15 AM
Comment:
Hi Azam,

I want to dynamically add a column of buttons (derived from the LinkButton class) that eventually cause a GirdView.RowCommand event. Any suggestions?
Subject: RE: Button field template
Name: AzamSharp
Date: 8/10/2007 2:18:14 PM
Comment:
Hi Hendrik,

You will need to call the GridViewTemplate constructor by providing the type of control that you want to create.
Subject: Binding programatically
Name: Sam
Date: 8/20/2007 12:53:13 PM
Comment:
Is there a way to create template columns dynamically, but then also programmatically have databound fields inside them, so that when I call databind, all the data gets in... without using a SQLDataSource?
Subject: Adding field at the end of gridview columns
Name: Narendra soni
Date: 9/26/2007 1:36:17 AM
Comment:
I bind my data source to my GridView. After then I've added two button column. I need the button columns appear at the end of my data source. But it appears at column no 1 and 2. What's the prob. with my code. Can u plz give me some solution?

GridView_Section = new GridView();
GridView_Section.ID = "GridView1";

GridView_Section.DataSource = dTable;

HyperLinkField _hLinkField = new HyperLinkField();
_hLinkField.HeaderText = "Edit";
_hLinkField.Text = "Edit...";
_hLinkField.NavigateUrl="my.aspx";
GridView_Section.Columns.Add(_hLinkField);

ButtonField _btnField = new ButtonField();
_btnField.HeaderText = "Delete";
_btnField.ButtonType = ButtonType.Image; GridView_Section.Columns.Add(_btnField);

GridView_Section.DataBind();
Thanx.
Subject: Dynamically Build Gridview Columns
Name: Greg
Date: 11/12/2007 1:10:09 PM
Comment:
Any help would be greatly appreciated!!!

I need to build a gridview that could change depending on what the user selects.

Example - of the what the stored procedure returns ->
Code
1| BEV011
2| BEV012
3| SAO011
4| FSS011
Etc....

I need to take the code field and make them into the the Column headings of my Gridview. I am able to do this, but it is also putting in Boundfields that I do not need. So from the example I put above, I am getting 4 columns and 4 rows. I do not want it to create the rows.

Any ideas?

Thanks!!!!
Subject: messing with sorting..
Name: nawaz
Date: 11/21/2007 1:32:27 AM
Comment:
Hello Azam,
My self is nawaz, i domostly use to see ur written codes and here i m messing with the problem of sorting when i am using template field in 3 tier architecture.
Every thingis cool with bound field but requirement is of template fiel
Subject: Dynamic BoundField
Name: Madhu Menon
Date: 1/7/2008 9:55:44 AM
Comment:
Hi Azam, Thank you for the wonderful article.
Subject: I need a soultion
Name: Rajesh Kumar
Date: 1/30/2008 6:59:34 AM
Comment:
Your code is great and it work perfect for my requirement.

I need a help, this is what I have done in GridViewTemplate.cs, using this Template I am trying to added a set of 3 (TextBox, Hidden, Hidden) controls for each ItemTemplate. My problem is that I am not able to get the value of these controls that are inside ItemTemplate on postback.
Can any one provide a solution for this problem

Thanks in advance.
Subject: gridview boundfield
Name: sajid
Date: 1/31/2008 3:41:07 AM
Comment:
Azam

Fantastic, thanks for sharing your vast knowledge on gridview
Subject: Great Job
Name: Uttam Mohanty
Date: 3/18/2008 1:59:55 PM
Comment:
It is a great job. I found different type code for showing the sort images but this is best and what I was looking for.

Thanks a lot for sharing this.
Subject: gridview example
Name: Magnus
Date: 3/22/2008 4:47:33 PM
Comment:
Thanks a lot buddy!
awesome! Looked everywhere for this example! Thanks again!
Subject: Reg : How do Write coding for Dynamically Edit/Update Gridview
Name: Mani
Date: 3/27/2008 2:37:56 AM
Comment:
Hi i saw ur articles very nice and excellent..i need some more help form ur side How do Write coding for Dynamically Edit/Update Gridview
Subject: hi
Name: sjk
Date: 4/2/2008 4:01:27 AM
Comment:
helpful...thanks a lot..
Subject: How to get the viewstate values
Name: Rajesh
Date: 4/2/2008 12:14:57 PM
Comment:
Hi,

Thanks for this solution. Here is my problem, I have created a dynamic ItemTemplate inside which textbox and hidden controls are created. I am not able to get the controls like textbox on postback, can you please guid to get a solution.

Thanks
Rajesh
Subject: RE: How to get the viewstate values
Name: AzamSharp
Date: 4/3/2008 2:33:00 PM
Comment:
Hi Rajesh, Since, you are creating the controls dynamically you will need to build the controls again on Page Load. Dynamic controls are lost when the page load occurs.
Subject: compliment
Name: bhushaiah
Date: 4/10/2008 5:40:27 AM
Comment:
nice article



Join WebHost4Life.com






Copyright GridViewGuy 2007-2008