模板类代码如下:
Code
1namespace Nimeux.Web.UI
2{
3 public class NimeuxTemplete : ITemplate
4 {
5 private string _colname;
6
7 /**//// <summary>
8 /// 构造函数
9 /// </summary>
10 /// <param name="colname">列的名字(对应数据库的字段)</param>
11 public NimeuxTemplete(string colname)
12 {
13 this._colname = colname;
14 }
15
16 /**//// <summary>
17 /// 实现接口ITemplate的方法
18 /// </summary>
19 /// <param name="container"></param>
20 /// <remarks>
21 /// 1、new一个LiteralControl
22 /// 2、给LiteralControl加一个绑定事件
23 /// 3、把该控件加到容器控件集合里
24 /// </remarks>
25 public void InstantiateIn(Control container)
26 {
27 LiteralControl l = new LiteralControl();
28 l.DataBinding += new EventHandler(this.OnDataBinding);
29 container.Controls.Add(l);
30 }
31
32 private void OnDataBinding(object sender, EventArgs e)
33 {
34 LiteralControl l = (LiteralControl)sender;
35
36 // 得到l的容器(父亲)——GridViewRow
37 GridViewRow container = (GridViewRow)l.NamingContainer;
38 // 通过列名(对应数据库的字段)给l赋值
39 l.Text = ((DataRowView)container.DataItem)[_colname].ToString();
40 }
41 }
42}
1namespace Nimeux.Web.UI
2{
3 public class NimeuxTemplete : ITemplate
4 {
5 private string _colname;
6
7 /**//// <summary>
8 /// 构造函数
9 /// </summary>
10 /// <param name="colname">列的名字(对应数据库的字段)</param>
11 public NimeuxTemplete(string colname)
12 {
13 this._colname = colname;
14 }
15
16 /**//// <summary>
17 /// 实现接口ITemplate的方法
18 /// </summary>
19 /// <param name="container"></param>
20 /// <remarks>
21 /// 1、new一个LiteralControl
22 /// 2、给LiteralControl加一个绑定事件
23 /// 3、把该控件加到容器控件集合里
24 /// </remarks>
25 public void InstantiateIn(Control container)
26 {
27 LiteralControl l = new LiteralControl();
28 l.DataBinding += new EventHandler(this.OnDataBinding);
29 container.Controls.Add(l);
30 }
31
32 private void OnDataBinding(object sender, EventArgs e)
33 {
34 LiteralControl l = (LiteralControl)sender;
35
36 // 得到l的容器(父亲)——GridViewRow
37 GridViewRow container = (GridViewRow)l.NamingContainer;
38 // 通过列名(对应数据库的字段)给l赋值
39 l.Text = ((DataRowView)container.DataItem)[_colname].ToString();
40 }
41 }
42}
然后,在页面中编写了一个私有方法如下:
Code
1/**//// <summary>
2 /// 根据不同参数显示不部内容
3 /// </summary>
4 /// <param name="flag">标记参数</param>
5 private void Show(int flag)
6 {
7 if (flag == 2)
8 {
9 TemplateField tf1 = new TemplateField();
10 NimeuxTemplete nt1 = new NimeuxTemplete("OldCard");
11 tf1.HeaderText = "旧卡号";
12 tf1.ItemTemplate = nt1;
13
14 TemplateField tf2 = new TemplateField();
15 NimeuxTemplete nt2 = new NimeuxTemplete("NewCard");
16 tf2.HeaderText = "新卡号";
17 tf2.ItemTemplate = nt2;
18
19 this.gvFSBT.Columns.Add(tf1);
20 this.gvFSBT.Columns.Add(tf2);
21 }
22 else
23 {
24 TemplateField tf1 = new TemplateField();
25 NimeuxTemplete nt1 = new NimeuxTemplete("NewCard");
26 tf1.HeaderText = "卡号";
27 tf1.ItemTemplate = nt1;
28
29 TemplateField tf2 = new TemplateField();
30 NimeuxTemplete nt2 = new NimeuxTemplete("ChangeContent");
31 tf2.HeaderText = "登记内容";
32 tf2.ItemTemplate = nt2;
33
34 this.gvFSBT.Columns.Add(tf1);
35 this.gvFSBT.Columns.Add(tf2);
36 }
37 }
1/**//// <summary>
2 /// 根据不同参数显示不部内容
3 /// </summary>
4 /// <param name="flag">标记参数</param>
5 private void Show(int flag)
6 {
7 if (flag == 2)
8 {
9 TemplateField tf1 = new TemplateField();
10 NimeuxTemplete nt1 = new NimeuxTemplete("OldCard");
11 tf1.HeaderText = "旧卡号";
12 tf1.ItemTemplate = nt1;
13
14 TemplateField tf2 = new TemplateField();
15 NimeuxTemplete nt2 = new NimeuxTemplete("NewCard");
16 tf2.HeaderText = "新卡号";
17 tf2.ItemTemplate = nt2;
18
19 this.gvFSBT.Columns.Add(tf1);
20 this.gvFSBT.Columns.Add(tf2);
21 }
22 else
23 {
24 TemplateField tf1 = new TemplateField();
25 NimeuxTemplete nt1 = new NimeuxTemplete("NewCard");
26 tf1.HeaderText = "卡号";
27 tf1.ItemTemplate = nt1;
28
29 TemplateField tf2 = new TemplateField();
30 NimeuxTemplete nt2 = new NimeuxTemplete("ChangeContent");
31 tf2.HeaderText = "登记内容";
32 tf2.ItemTemplate = nt2;
33
34 this.gvFSBT.Columns.Add(tf1);
35 this.gvFSBT.Columns.Add(tf2);
36 }
37 }
这样根据flag传递的参数就可以显示不同的内容了。GridView通过模板动态添加项应用非常多,我这里只是根据一些要求做简单的应用罢了。为了使自己的博客充实一点,就写了下了。