国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

Creating DataGrid Templated Columns Dynamically -

2024-07-21 02:22:02
字體:
供稿:網(wǎng)友

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. 

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 咸丰县| 金阳县| 科技| 嘉峪关市| 黑河市| 东宁县| 荆门市| 曲沃县| 济源市| 拜城县| 大足县| 奈曼旗| 九龙坡区| 神池县| 封开县| 库车县| 曲麻莱县| 明光市| 永春县| 镇康县| 太保市| 比如县| 大竹县| 广西| 六盘水市| 石屏县| 扎囊县| 望奎县| 石嘴山市| 永胜县| 漳浦县| 佛冈县| 青铜峡市| 永康市| 仪陇县| 历史| 瑞丽市| 易门县| 金川县| 三门县| 渝北区|