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 asked whether we can do similar thing with templated columns. This two part article explains just that. There are actually two ways to create DataGrid templated columns dynamically - using LoadTemplate method and implementing ITemplate interface. In Part - I, we will discuss how to use the first method i.e. using LoadTemplate.

What are templates anyway?

ASP.NET list controls like DataGrid, DataList and Repeater allow flexibility in changing look and feel via the use of what is called as 'Templates'. As the name suggests template is a blue print of look and feel for data displayed in certain areas of the control. For example, HeaderTemplate represents blue print of the header of the control. Similarly, ItemTemplate represents individual item or row of the DataGrid. These sectons are represented by markup like <ItemTemplate> and <HeaderTemplate>.

LoadTemplate method

If you see the class hierarchy of Page class it looks like this:

System.Object   System.Web.UI.Control      System.Web.UI.TemplateControl         System.Web.UI.Page

The System.Web.UI.TemplateControl class contains a method called LoadTemplate that can be used to dynamially load temlates for templated controls. The signature of LoadTemplate is like this:

public ITemplate LoadTemplate(string virtualPath);

The method takes virtual path of a user control file i.e. .ascx file.

Creating user control file for template markup

You should store markup of the template in .ascx file. Remember that the markup should not contain actual tags like <ItemTemplate>. Typically this file will not be having any other markup. Note that one such file represents markup for one kind of template.

Example of using LoadTemplate method

We will now develop an example in which we will create a DataGrid on the fly with a single template column. The template column simply contains a Label control but you can add any other controls as well. Our example will need to follow steps given below:

  • Create a new ASP.NET web application
  • Create a new web form
  • Create a user control file (.ascx) for template markup
  • Write code to add a DataGrid to the form
  • Load the template from the .ascxfile
  • Bind the DataGrid with some datasource

Out of above steps first two are common to any web application and we will not discuss them here. So, let us start by creating a user control file for our template.

User control file for our example

You may add a new web user control (.ascx) to your project. Add following markup to the file:

<%@ Control Language="C#" %><asp:label ID="label1" Runat="server" text='<%# Databinder.eval(ctype(container,DataGridItem).dataitem,"lastname")%>'></asp:label>

Note that this markup looks same as if we would have used it in design mode. Here, we are binding a label control with the lastname field from the DataMember.

Adding DataGrid to the form

Add following code to the Page_Load event of the web form

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");ITemplate temp= Page.LoadTemplate("webusercontrol1.ascx");TemplateColumn tc=new TemplateColumn();tc.HeaderText = "Last Name";tc.ItemTemplate = temp;DataGrid1.Columns.Add(tc);DataGrid1.DataSource = ds;DataGrid1.DataMember = "employees";DataGrid1.DataBind();

Most of the code will be familiar to you except code related to ITemplate. We have loaded our template using LoadTemplate method. We then create a TemplateColumn and set its ItemTemplate to it. Even though we have used ItemTemplate, it can be any other type of template also like EditItemTemplate. We then add this new column to the DataGrid. Note that our DataGrid instance DataGrid1 is declared at class level and hence not visible in the code shown above.
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 LoadTemplate method. This method can be used if you want to use same DataGrid with say few columns changing at run time. In the next part we will see how to create templated columns via implementing ITemplate interface. Till then keep coding!

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

时间: 2024-11-03 08:20:07

Creating DataGrid Templated Columns Dynamically - Part I(转自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 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

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的列了,列中还可以添加模版列,对应每一模版列