通过实现ITemplate接口编程动态添加模板列
使用编程方式动态添加DataGrid模板列:
public class UserItemTemplate : ITemplate {
#region ITemplate 成员
public void InstantiateIn(Control container) {
Label lbl = new Label();
lbl.DataBinding += new EventHandler(lbl_DataBinding);
container.Controls.Add( lbl );
}
#endregion
private void lbl_DataBinding(object sender, EventArgs e) {
Label lbl = (Label)sender;
if( DataBinder.Eval( ((DataGridItem)lbl.NamingContainer).DataItem , "HasPayPoint" ).ToString() == "True" ){
lbl.Text = DataBinder.Eval( ((DataGridItem)lbl.NamingContainer).DataItem , "UserName" ) + "<img src='" + FC.CMS.Utility.Path.ApplicationFileHttpUrl + "modules/auto/GroupBuy/IsMember.gif' alt='已支付保证金会员'>";
}
else
lbl.Text = DataBinder.Eval( ((DataGridItem)lbl.NamingContainer).DataItem , "UserName" ).ToString();
}
}
...
TemplateColumn col1 = new TemplateColumn();
col1.HeaderText = "用户名";
col1.ItemStyle.CssClass = "Normal";
col1.ItemTemplate = new UserItemTemplate() ;
UserList.Columns.Add( col1 );
...