首页 > 开发 > 综合 > 正文

Creating DataGrid Templated Columns Dynamically -

2024-07-21 02:16:54
字体:
来源:转载
供稿:网友

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!
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表