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.
About the author

Name :

Bipin Joshi

Email :

webmaster@dotnetbips.com

Profile :

Bipin Joshi - the creator and owner of DotNetBips - is a Microsoft MVP, Software Developer and Author. He has written dozens of articles for DotNetBips and other web sites. Know more about him and DotNetBips here.

<div style=";text-align:center;;height:auto;" class="datagrid-cell datagrid-cell-c1-checkId">已通过</div></td><td field="button"><div style=";height:auto;" class="datagrid-cell datagrid-cell-c1-button"><a href="#" style="color: red" onclick="fileManager(0)">图片管理</a></div></td><td field="truckNo"><div style=";height:auto;" class="datagrid-cell datagrid-cell-c1-truckNo">辽PD6885</div></td><td field="truckCardColor"><div style=";text-align:center;;height:auto;" class="datagrid-cell datagrid-cell-c1-truckCardColor">黄牌</div></td><td field="vtNam"><div style=";height:auto;" class="datagrid-cell datagrid-cell-c1-vtNam">秦皇岛九福物流有限公司</div></td><td field="driverNam"><div style=";height:auto;" class="datagrid-cell datagrid-cell-c1-driverNam">叶红建</div></td><td field="linkTel"><div style=";height:auto;" class="datagrid-cell datagrid-cell-c1-linkTel">13842929049</div></td><td field="workCompanyCod"><div style=";text-align:center;;height:auto;" class="datagrid-cell datagrid-cell-c1-workCompanyCod">金海粮油</div></td><td field="cargoNam" style="display:none;"><div style=";height:auto;" class="datagrid-cell datagrid-cell-c1-cargoNam"></div></td><td field="consignCod" style="display:none;"><div style=";height:auto;" class="datagrid-cell datagrid-cell-c1-consignCod"></div></td><td field="planDte"><div style=";height:auto;" class="datagrid-cell datagrid-cell-c1-planDte">2023-05-01</div></td><td field="validTyp"><div style=";height:auto;" class="datagrid-cell datagrid-cell-c1-validTyp">当天有效</div></td><td field="ifEnd"><div style=";height:auto;" class="datagrid-cell datagrid-cell-c1-ifEnd">x</div></td><td field="individualId" style="display:none;"><div style=";height:auto;" class="datagrid-cell datagrid-cell-c1-individualId">0</div></td><td field="rejectReason"><div style=";height:auto;" class="datagrid-cell datagrid-cell-c1-rejectReason"></div></td><td field="checkNam"><div style=";height:auto;" class="datagrid-cell datagrid-cell-c1-checkNam">jhly</div></td><td field="checkTim"><div style=";height:auto;" class="datagrid-cell datagrid-cell-c1-checkTim">2023-04-29 21:09</div></td>以上代码为网页源码,帮我写一段python程序从以上代码中找出drivernam和checkTim并保存数据库中
最新发布
06-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值