今天看到一篇文章,提出使用RenderMethod委托可以实现DataGrid表头的合并。自己测试了一下,效果不错,把DataGrid的表头转化为用Table来控制,就可以随心所欲了。可能前面有人用这种方法是实现过,但是我还是把代码贴上来,大家分享一下
1using System;
2using System.Collections;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Web;
7using System.Web.SessionState;
8using System.Web.UI;
9using System.Web.UI.WebControls;
10using System.Web.UI.HtmlControls;
11using System.Data.SqlClient;
12
13namespace WebDataGridHeader
14{
15 /// <summary>
16 ///DataGrid表头合并问题
17 /// </summary>
18 public class WebForm1 : System.Web.UI.Page
19 {
20 protected System.Web.UI.WebControls.DataGrid DataGrid1;
21 protected System.Web.UI.WebControls.Label Label1;
22
23 private void Page_Load(object sender, System.EventArgs e)
24 {
25 // 在此处放置用户代码以初始化页面
26 string m_strConn = "server=.;uid=sa;pwd=sa;database=Northwind";
27 SqlConnection conn = new SqlConnection(m_strConn);
28
29 try
30 {
31 conn.Open();
32
33 SqlCommand cmd = new SqlCommand("SELECT * FROM Employees",conn);
34
35 SqlDataAdapter adp = new SqlDataAdapter(cmd);
36
37 DataTable dt = new DataTable();
38 adp.Fill(dt);
39
40 this.DataGrid1.DataSource = dt;
41 this.DataGrid1.DataBind();
42 }
43 catch(Exception ex)
44 {
45 throw ex;
46 }
47 finally
48 {
49 conn.Close();
50 }
51 }
52
53 Web 窗体设计器生成的代码
74
75 /// <summary>
76 /// 创建Item
77 /// </summary>
78 /// <param name="sender"></param>
79 /// <param name="e"></param>
80 private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
81 {
82 //将Item的呈现方法定向到自定义的呈现方法上
83 ListItemType lit = e.Item.ItemType;
84 if(ListItemType.Header == lit)
85 {
86 e.Item.SetRenderMethodDelegate(new RenderMethod(NewRenderMethod));
87 }
88 }
89
90 /// <summary>
91 /// 自定义的Item呈现方法
92 /// </summary>
93 /// <param name="writer"></param>
94 /// <param name="ctl"></param>
95 private void NewRenderMethod(HtmlTextWriter writer,Control ctl)
96 {
97 //不需要从<TR>标签开始
98 //输出“联系电话”列
99 writer.Write("<TD colspan=\"3\" align=\"center\">联系电话</TD>\n");
100
101 //“地址”列必须有rowspan属性且必须在第一列呈现
102 TableCell cell = (TableCell)ctl.Controls[ctl.Controls.Count - 1];
103 cell.Attributes.Add("rowspan","2");
104 cell.RenderControl(writer);
105
106 //现在关闭第一行
107 writer.Write("</TR>\n");
108
109 //将设计时的样式属性添加到第二行使得两行的外观相似
110 this.DataGrid1.HeaderStyle.AddAttributesToRender(writer);
111
112 //插入第二行
113 writer.RenderBeginTag("TR");
114
115 //呈现除了最后一列(刚才已经呈现过了)外的所有在设计时定义的cells
116 for(int i=0;i<=ctl.Controls.Count-2;i++)
117 {
118 ctl.Controls[i].RenderControl(writer);
119 }
120
121 //不需要以</TR>结束
122 }
123 }
124}
2using System.Collections;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Web;
7using System.Web.SessionState;
8using System.Web.UI;
9using System.Web.UI.WebControls;
10using System.Web.UI.HtmlControls;
11using System.Data.SqlClient;
12
13namespace WebDataGridHeader
14{
15 /// <summary>
16 ///DataGrid表头合并问题
17 /// </summary>
18 public class WebForm1 : System.Web.UI.Page
19 {
20 protected System.Web.UI.WebControls.DataGrid DataGrid1;
21 protected System.Web.UI.WebControls.Label Label1;
22
23 private void Page_Load(object sender, System.EventArgs e)
24 {
25 // 在此处放置用户代码以初始化页面
26 string m_strConn = "server=.;uid=sa;pwd=sa;database=Northwind";
27 SqlConnection conn = new SqlConnection(m_strConn);
28
29 try
30 {
31 conn.Open();
32
33 SqlCommand cmd = new SqlCommand("SELECT * FROM Employees",conn);
34
35 SqlDataAdapter adp = new SqlDataAdapter(cmd);
36
37 DataTable dt = new DataTable();
38 adp.Fill(dt);
39
40 this.DataGrid1.DataSource = dt;
41 this.DataGrid1.DataBind();
42 }
43 catch(Exception ex)
44 {
45 throw ex;
46 }
47 finally
48 {
49 conn.Close();
50 }
51 }
52
53 Web 窗体设计器生成的代码
74
75 /// <summary>
76 /// 创建Item
77 /// </summary>
78 /// <param name="sender"></param>
79 /// <param name="e"></param>
80 private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
81 {
82 //将Item的呈现方法定向到自定义的呈现方法上
83 ListItemType lit = e.Item.ItemType;
84 if(ListItemType.Header == lit)
85 {
86 e.Item.SetRenderMethodDelegate(new RenderMethod(NewRenderMethod));
87 }
88 }
89
90 /// <summary>
91 /// 自定义的Item呈现方法
92 /// </summary>
93 /// <param name="writer"></param>
94 /// <param name="ctl"></param>
95 private void NewRenderMethod(HtmlTextWriter writer,Control ctl)
96 {
97 //不需要从<TR>标签开始
98 //输出“联系电话”列
99 writer.Write("<TD colspan=\"3\" align=\"center\">联系电话</TD>\n");
100
101 //“地址”列必须有rowspan属性且必须在第一列呈现
102 TableCell cell = (TableCell)ctl.Controls[ctl.Controls.Count - 1];
103 cell.Attributes.Add("rowspan","2");
104 cell.RenderControl(writer);
105
106 //现在关闭第一行
107 writer.Write("</TR>\n");
108
109 //将设计时的样式属性添加到第二行使得两行的外观相似
110 this.DataGrid1.HeaderStyle.AddAttributesToRender(writer);
111
112 //插入第二行
113 writer.RenderBeginTag("TR");
114
115 //呈现除了最后一列(刚才已经呈现过了)外的所有在设计时定义的cells
116 for(int i=0;i<=ctl.Controls.Count-2;i++)
117 {
118 ctl.Controls[i].RenderControl(writer);
119 }
120
121 //不需要以</TR>结束
122 }
123 }
124}