Creating DataGrid Templated Columns Dynamically - Part II(转自DotNetTips)

datagrid

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. 

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索class
, datagrid
, this
, column
The
templated、comtemplated、dynamically、dynamically linked、dynamicallyinvokable,以便于您获取更多的相关知识。

时间: 2024-10-19 17:44:27

Creating DataGrid Templated Columns Dynamically - Part II(转自DotNetTips)的相关文章

Creating DataGrid Templated Columns Dynamically -

Creating DataGrid Templated Columns Dynamically - Part I Introduction Few months back I wrote article on how to create DataGrid programatically. The article explained how to created a DataGrid with bound columns on the fly. Many readers asked whether

Creating DataGrid Templated Columns Dynamically - Part I(转自Dotnettips)

datagrid Creating DataGrid Templated Columns Dynamically - Part I Introduction Few months back I wrote article on how to create DataGrid programatically. The article explained how to created a DataGrid with bound columns on the fly. Many readers aske

DPC:Hiding Columns In A DataGrid[等级:初 中]

datagrid Hiding Columns In A DataGrid One of the frequently asked questions over at Asplists.com is: "How do I hide a column in a datagrid?". One very important point to note is that you cannot hide autogenerated columns in a data grid. The reas

常见Datagrid错误

datagrid|错误 摘要:学习如何避免在使用 ASP.NET Datagrid 控件进行开发时可能发生的一些常见错误(本文包含一些指向英文站点的链接). Datagrid 控件是 Microsoft ASP.NET 中功能最强.用途最广的 Web 控件之一,这一点已经得到了 ASP.NET 权威人士的认同.虽然 Datagrid 控件易于使用,但同样易于给使用者带来麻烦.以下是许多人所犯的一些错误,这些人包括从初学者到富有经验的 .NET 专家.您可以看到许多苦闷的使用者在 ASP.NET

常见 Datagrid 错误

datagrid|错误 摘要:学习如何避免在使用 ASP.NET Datagrid 控件进行开发时可能发生的一些常见错误(本文包含一些指向英文站点的链接). Datagrid 控件是 Microsoft ASP.NET 中功能最强.用途最广的 Web 控件之一,这一点已经得到了 ASP.NET 权威人士的认同.虽然 Datagrid 控件易于使用,但同样易于给使用者带来麻烦.以下是许多人所犯的一些错误,这些人包括从初学者到富有经验的 .NET 专家.您可以看到许多苦闷的使用者在 ASP.NET

ASP.NET中Datagrid常见错误

asp.net|datagrid|错误 摘要:学习如何避免在使用 ASP.NET Datagrid 控件进行开发时可能发生的一些常见错误. Datagrid 控件是 Microsoft? ASP.NET 中功能最强.用途最广的 Web 控件之一,这一点已经得到了 ASP.NET 权威人士的认同.虽然 Datagrid 控件易于使用,但同样易于给使用者带来麻烦.以下是许多人所犯的一些错误,这些人包括从初学者到富有经验的 .NET 专家.您可以看到许多苦闷的使用者在 ASP.NET 新闻组和论坛就这

『WPF』DataGrid的使用

原文 『WPF』DataGrid的使用 几点说明 这里主要是参考了MSDN中关于DataGrid的说明 这里只会简单说明在WPF中,DataGird最简单的使用方法 对于MSDN中的翻译不会很详细,也不会每一句都翻译.   来自MSDN的内容 Type Name Description Constructors DataGrid Initializes a new instance of the System.Windows.Controls.DataGrid class. Property I

Flex2 发现之旅:动态创建DataGrid列

datagrid|创建|动态 Flex2中,DataGrid如果我们没有指定columns熟悉的话,DataGrid会自动根据dataProvider的各行数据的属性名隐式自动地创建列,例如如下代码:<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.macromedia.com/2005/mxml" xmlns="*&

集各种功能于一身的DataGrid

datagrid RequiredFieldValidator控件用来校验一个输入框中是否输入了值,RegularExpressionValidator控件用来进行正则表达是的匹配.关于正则表达式的说明请参阅其他资料. 其中ControlToValidate属性就是需要校验的文本框的ID号.标签中的文本就是在校验不成功的时候显示出来的提示,Display属性则是提示信息的显示方式. DataGrid中<Columns>标签内的内容就是DataGrid的列了,列中还可以添加模版列,对应每一模版列