Creating DataGrid Templated Columns Dynamically - Part II(转自DotNetTips)
Creating DataGrid Templated Columns Dynamically - Part II
Introduction
In previous part of this article we saw how to use LoadTemplate method to dynamically add templated columns to the DataGrid. In this part we will see how to do that using ITemplate interface.ITemplate Interface
This interface found in System.Web.UI namespace has one method with following signature. void InstantiateIn(Control container);This method must be implemented in order to decide @#parent@# of the template.Implementing ITemplate interface
Let us start by creating our own implementation of ITemplate. Create a new class and add following code to it: using System;using System.Web.UI;using System.Web.UI.WebControls;using System.Data;namespace DynamicDataGridTemplates{public class CTemplateColumn:ITemplate{ private string colname; public CTemplateColumn(string cname) { colname=cname; } //must implement following method public void InstantiateIn(Control container) { LiteralControl l = new LiteralControl(); l.DataBinding += new EventHandler(this.OnDataBinding); container.Controls.Add(l); } public void OnDataBinding(object sender, EventArgs e) { LiteralControl l = (LiteralControl) sender; DataGridItem container = (DataGridItem) l.NamingContainer; l.Text = ((DataRowView) container.DataItem)[colname].ToString(); }}}Here, the constructor accepts the column name to which we want to bind our templated column. We have created a literal control in the InstantiateIn method. Since our column will be data bound we add OndataBinding event handler that populates the control with the appropriate values. Then we add this literalcontrol to the container@#s controls collection. In the OnDataBinding event handler the NamingContainer gives the current DataGridItem (since our parent control is DataGrid).Adding a template column to the DataGrid
Now, let us use our implementation of ITemplate interface to add a templated column to the DataGrid. Add following code in the page_Load event. DataGrid datagrid1=new DataGrid();TemplateColumn tc1=new TemplateColumn();tc1.ItemTemplate=new CTemplateColumn("lastname");tc1.HeaderText="Last Name";datagrid1.Columns.Add(tc1);Page.Controls[1].Controls.Add(datagrid1);string connstr = @"Integrated Security=SSPI;User ID=sa;Initial Catalog=Northwind;Data Source=MyServer\NetSDK";SqlConnection cnn=new SqlConnection(connstr);SqlDataAdapter da=new SqlDataAdapter("select * from employees", cnn)DataSet ds=new DataSet();da.Fill(ds, "employees");datagrid1.DataSource = ds;datagrid1.DataMember = "employees";datagrid1.DataBind();Here, we have create instance of DataGrid class. We have crerated instance of TemplateColumn class. Our intention is to add a templated column whose ItemTemplate is as decided by our class. Hence we set ItemTemplate property. In the same manner you can also set EditItemTemplate. We have passed the column name (lastname) in the constructor of this class. We then add this column to the DataGrid and DataGrid to the page. Binding of DataGrid follows as usual.After running your application you should get a DataGrid with single templated column titled @#Last Name@#.
Summary
In this article we saw how to add a templated column to a DataGrid on the fly using ITemplate interface. We created a custom class that implemented this interface We then set this class as ItemTemplate for the DataGrid. As you see this method though a bit complex gives more overall control on the process.下一篇:用ASP.NET写你自己的代码生成器(2)。 >>
相关文章:
- · 有空的时候看看,:)ASP.NET Page Templates
- · VB.NET开发互联网应用
- · vb.net cookie操作
- · Net中如何操作IIS(原理篇)
- · 关于选用何种ASP.NET设计方法的技巧
- · .Net中如何操作IIS(源代码) (原创)
- · iis 坏掉了,重新安装了以后.netframework 不能用了的解决方法
- · 两个aspx页面间传递引用对象。
- · 在Webcontrol的Toolbar上加入删除确认的方法(改进后)
- · TreeView 派生类: TreeViewEx 实现 NodeShowToolTip、NodeDoubleClick 事件
- · 我自己写的自定义Web的上传控件
- · 增加判断文字长度,汉字算2个
- · 客户端脚本对中文的验证(javascript)
- · 献丑了,我的asp.net网站开发经验,欢迎参加讨论。
- · 笑望人生,关于IHttpHandler处理图片
- · HTML在线编辑器--服务器控件~~.NET实现~~
- · How to Share Session State Between Classic ASP and ASP.NET(1)
- · How to Share Session State Between Classic ASP and ASP.NET(2)
- · 关于验证控件,希望对和我原来有疑惑的朋友有帮助(刚找的资料,结合猫猫的)
- · 上次的一个问题我打了微软的求助电话,他们也没有办法!
- · [技巧]DataGird的hyper column的url field 绑定两个字段
- · ms--help
- · 续
- · Simple Paging in Repeater and DataList Controls
- · ASP.NET编程中的十大技巧(建议进精华)
- · 转贴:DataGrid/DataList
- · 用ASP.NET写你自己的代码生成器(1)。
- · ASP.NET中Cookie编程的基础知识(6)
- · ASP.NET中Cookie编程的基础知识(5)
- · ASP.NET中Cookie编程的基础知识(4)
- · ASP.NET中Cookie编程的基础知识(3)
- · ASP.NET中Cookie编程的基础知识(2)
- · ASP.NET中Cookie编程的基础知识(1)
- · .NET中窗体间相互访问的几种方式
- · .net中PictureBox中图片的拖动
- · 在.NET上如何根据字符串动态创建控件
- · .NET 窗体之间的交互
- · 使用UltraWinGrid时双击的处理
