Dynamically Creating Bound and Template Columns in GridView

原创 2007年09月23日 20:51:00

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
Download Source Code

 

http://gridviewguy.com/ArticleDetails.aspx?articleID=88

Dynamically creating bound and template columns in GridView using ASP.Net

In some complex scenarios developers need to create runtime GridView dynamically. So obviously devel...
  • L_Vincent
  • L_Vincent
  • 2011年08月29日 14:19
  • 416

Dynamically Adding Template columns to a GridView

IntroductionMany a times we wonder how to add template column dynamically. This might be the case wh...
  • Titan_koa
  • Titan_koa
  • 2007年02月08日 16:46
  • 558

Creating DataGrid Templated Columns Dynamically -

  • zgqtxwd
  • zgqtxwd
  • 2008年05月01日 04:21
  • 90

webwervice发布时出错 java.security.PrivilegedActionException

错误信息: 信息: Dynamically creating response wrapper bean Class com.potevio.ws.jaxws.DealReqResponse ...
  • zc707212993
  • zc707212993
  • 2013年08月26日 21:05
  • 780

Creating DataGrid Templated Columns Dynamically - Part II

IntroductionIn previous part of this article we saw how to use LoadTemplate method to dynamically ad...
  • qieyj
  • qieyj
  • 2003年04月08日 09:05
  • 914

Creating DataGrid Templated Columns Dynamically - Part I

IntroductionFew months back I wrote article on how to create DataGrid programatically. The article e...
  • qieyj
  • qieyj
  • 2003年04月08日 09:05
  • 1040

GridView.Columns 属性

一个列字段,表示 GridView 控件中的一个列。Columns 属性(集合)用来存储呈现在 GridView 控件中的所有显式声明的列字段。您还可以使用 Columns 集合以编程方式管理列字段集...
  • mane_yao
  • mane_yao
  • 2010年07月20日 16:58
  • 2720

Creating GridView Columns Dynamically (Part 1)--动态创建GridView的列(一)

 导读:Beginning with this article I am starting a series that will show you how to create data bound c...
  • kucool
  • kucool
  • 2007年09月28日 15:50
  • 1335

DEBUG 14075 [freemark] (): Could not find template in cache, creating new one;

要把log4j这样的jar包, log4j.properties, struts.properties加入一下,struts.properties在src下,...
  • petercnmei
  • petercnmei
  • 2014年08月14日 10:23
  • 3125

模板类和友元函数再探

C++友元函数
  • markjenny
  • markjenny
  • 2016年04月13日 15:46
  • 372
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Dynamically Creating Bound and Template Columns in GridView
举报原因:
原因补充:

(最多只允许输入30个字)