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

相关文章推荐

Creating and Training Traditional Template Square Markers

创建并训练传统的Template Square Marker  Marker是ARToolKit 的光学输入信号。传统的Template Square Marker 是一种能够被ARToolKit 在...

Editable GridView - All Columns in GridView are Editable

Summary : Usually developers do not use an editable GridView for many reasons. Developers prefer...

creating-skeletal-animation-in-blender-and-exporting-it-to-three-js

CREATING SKELETAL ANIMATION IN BLENDER AND EXPORTING IT TO THREE.JS. UPDATE: Originally this tu...

Class method differences in Python: bound, unbound and static

原文出处:http://stackoverflow.com/questions/114214/class-method-differences-in-python-bound-unbound-and-...
  • xluren
  • xluren
  • 2015年05月04日 09:39
  • 482

Creating Configurable Environments with Hierarchical and Global Options (vmm_opts) in VMM

Verification environments need to accommodate changing specifications and at  the same time cater t...

Dropping and creating tables in read only tablespaces?!

You probably already know that it’s possible to drop tables in Oracle read only tablespaces… (You di...
  • loryliu
  • loryliu
  • 2015年12月14日 22:03
  • 290

Tutorial: Creating models in 3dsMa and Render via JPCT-Bones

The JPCT-Bones can be used forrendering Skeleton Model and the workflow can be described as follow: ...

Creating and Using Your Own Delegates in Objective-C

I've been doing iPhone development for about a year now and have used the delegation pattern many ti...
  • gdmmhym
  • gdmmhym
  • 2011年09月01日 10:50
  • 252

Creating Web Applications in JavaScript with Express.js and ejs

用node.js + express.js + ejs显示网页文章来源:http://www.daniweb.com/web-development/javascript-dhtml-ajax/rev...
  • macyang
  • macyang
  • 2011年04月11日 19:56
  • 1291
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Dynamically Creating Bound and Template Columns in GridView
举报原因:
原因补充:

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