博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Dynamically Creating Bound and Template Columns in GridView
阅读量:4321 次
发布时间:2019-06-06

本文共 2372 字,大约阅读时间需要 7 分钟。

Dynamically Creating Bound and Template Columns in GridView
By

 

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 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 .

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

转载于:https://www.cnblogs.com/miaomiaoga/archive/2007/01/04/611013.html

你可能感兴趣的文章
二. python数组和列表
查看>>
七. k8s--volumes之pv pvc学习笔记
查看>>
八. k8s--configmap学习笔记
查看>>
十. k8s--访问控制 serviceaccount和RBAC 学习笔记
查看>>
九. k8s--statefulset控制器
查看>>
十一. k8s--dashboard部署
查看>>
shell解析xml文件
查看>>
十二. k8s--网络策略flannel与canal学习笔记
查看>>
十三. k8s--调度器
查看>>
十四. k8s资源需求和限制, 以及pod驱逐策略
查看>>
三. k8s基本操作以及pod存活以及可用性验证钩子
查看>>
五. k8s--service学习笔记
查看>>
二. k8s安装过程
查看>>
jenkins pipeline 使用遇到的问题
查看>>
四. k8s--pod控制器
查看>>
一. python数据结构与算法
查看>>
django模型内部类meta解释
查看>>
v-for(:key)绑定index、id、key的区别
查看>>
el-tree文本内容过多显示不完全问题(解决)
查看>>
el-table翻页序号不从1开始(已解决)
查看>>