今天看到一篇文章,提出使用RenderMethod委托可以实现DataGrid表头的合并。自己测试了一下,效果不错,把DataGrid的表头转化为用Table来控制,就可以随心所欲了。可能前面有人用这种方法是实现过,但是我还是把代码贴上来,大家分享一下
1
using System;
2
using System.Collections;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Web;
7
using System.Web.SessionState;
8
using System.Web.UI;
9
using System.Web.UI.WebControls;
10
using System.Web.UI.HtmlControls;
11
using System.Data.SqlClient;
12![](/Images/OutliningIndicators/None.gif)
13
namespace 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![](/Images/OutliningIndicators/InBlock.gif)
33
SqlCommand cmd = new SqlCommand("SELECT * FROM Employees",conn);
34
35
SqlDataAdapter adp = new SqlDataAdapter(cmd);
36![](/Images/OutliningIndicators/InBlock.gif)
37
DataTable dt = new DataTable();
38
adp.Fill(dt);
39![](/Images/OutliningIndicators/InBlock.gif)
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![](/Images/OutliningIndicators/InBlock.gif)
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![](/Images/OutliningIndicators/InBlock.gif)
101
//“地址”列必须有rowspan属性且必须在第一列呈现
102
TableCell cell = (TableCell)ctl.Controls[ctl.Controls.Count - 1];
103
cell.Attributes.Add("rowspan","2");
104
cell.RenderControl(writer);
105![](/Images/OutliningIndicators/InBlock.gif)
106
//现在关闭第一行
107
writer.Write("</TR>\n");
108![](/Images/OutliningIndicators/InBlock.gif)
109
//将设计时的样式属性添加到第二行使得两行的外观相似
110
this.DataGrid1.HeaderStyle.AddAttributesToRender(writer);
111![](/Images/OutliningIndicators/InBlock.gif)
112
//插入第二行
113
writer.RenderBeginTag("TR");
114![](/Images/OutliningIndicators/InBlock.gif)
115
//呈现除了最后一列(刚才已经呈现过了)外的所有在设计时定义的cells
116
for(int i=0;i<=ctl.Controls.Count-2;i++)
117
{
118
ctl.Controls[i].RenderControl(writer);
119
}
120![](/Images/OutliningIndicators/InBlock.gif)
121
//不需要以</TR>结束
122
}
123
}
124
}
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/None.gif)
8
![](/Images/OutliningIndicators/None.gif)
9
![](/Images/OutliningIndicators/None.gif)
10
![](/Images/OutliningIndicators/None.gif)
11
![](/Images/OutliningIndicators/None.gif)
12
![](/Images/OutliningIndicators/None.gif)
13
![](/Images/OutliningIndicators/None.gif)
14
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
15
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
31
![](/Images/OutliningIndicators/InBlock.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/InBlock.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/InBlock.gif)
36
![](/Images/OutliningIndicators/InBlock.gif)
37
![](/Images/OutliningIndicators/InBlock.gif)
38
![](/Images/OutliningIndicators/InBlock.gif)
39
![](/Images/OutliningIndicators/InBlock.gif)
40
![](/Images/OutliningIndicators/InBlock.gif)
41
![](/Images/OutliningIndicators/InBlock.gif)
42
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
43
![](/Images/OutliningIndicators/InBlock.gif)
44
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
45
![](/Images/OutliningIndicators/InBlock.gif)
46
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
47
![](/Images/OutliningIndicators/InBlock.gif)
48
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
49
![](/Images/OutliningIndicators/InBlock.gif)
50
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
51
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
52
![](/Images/OutliningIndicators/InBlock.gif)
53
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
74
![](/Images/OutliningIndicators/InBlock.gif)
75
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
76
![](/Images/OutliningIndicators/InBlock.gif)
77
![](/Images/OutliningIndicators/InBlock.gif)
78
![](/Images/OutliningIndicators/InBlock.gif)
79
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
80
![](/Images/OutliningIndicators/InBlock.gif)
81
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
82
![](/Images/OutliningIndicators/InBlock.gif)
83
![](/Images/OutliningIndicators/InBlock.gif)
84
![](/Images/OutliningIndicators/InBlock.gif)
85
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
86
![](/Images/OutliningIndicators/InBlock.gif)
87
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
88
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
89
![](/Images/OutliningIndicators/InBlock.gif)
90
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
91
![](/Images/OutliningIndicators/InBlock.gif)
92
![](/Images/OutliningIndicators/InBlock.gif)
93
![](/Images/OutliningIndicators/InBlock.gif)
94
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
95
![](/Images/OutliningIndicators/InBlock.gif)
96
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
97
![](/Images/OutliningIndicators/InBlock.gif)
98
![](/Images/OutliningIndicators/InBlock.gif)
99
![](/Images/OutliningIndicators/InBlock.gif)
100
![](/Images/OutliningIndicators/InBlock.gif)
101
![](/Images/OutliningIndicators/InBlock.gif)
102
![](/Images/OutliningIndicators/InBlock.gif)
103
![](/Images/OutliningIndicators/InBlock.gif)
104
![](/Images/OutliningIndicators/InBlock.gif)
105
![](/Images/OutliningIndicators/InBlock.gif)
106
![](/Images/OutliningIndicators/InBlock.gif)
107
![](/Images/OutliningIndicators/InBlock.gif)
108
![](/Images/OutliningIndicators/InBlock.gif)
109
![](/Images/OutliningIndicators/InBlock.gif)
110
![](/Images/OutliningIndicators/InBlock.gif)
111
![](/Images/OutliningIndicators/InBlock.gif)
112
![](/Images/OutliningIndicators/InBlock.gif)
113
![](/Images/OutliningIndicators/InBlock.gif)
114
![](/Images/OutliningIndicators/InBlock.gif)
115
![](/Images/OutliningIndicators/InBlock.gif)
116
![](/Images/OutliningIndicators/InBlock.gif)
117
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
118
![](/Images/OutliningIndicators/InBlock.gif)
119
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
120
![](/Images/OutliningIndicators/InBlock.gif)
121
![](/Images/OutliningIndicators/InBlock.gif)
122
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
123
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
124
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)