因为DataGrid控件的简单易懂,大多数做.NET程序员都喜欢用,有需要把数据显示成表格样式的地方DataGrid会是首选,但是所有的东西都会有好和不好的一面,DataGrid在给我们带来了数据显示方便的同时,也给我们带来了不灵活的问题,多层表头就是例子。
相信大家都遇到过这样的需求:
Head1
|
Head2
|
Head3
|
Head4
|
|
SubTitle1
|
SubTitle2
|
我在网上Google和Baidu了一下,发现大家都是在DataGrid的ItemCreated和ItemDataBind事件中来实现的(实现的方法太多,这里就不用列举了),我觉得这样做效率不高而且麻烦,因为需要在系统中每个用到DataGrid的地方都需要添加那样的实现代码。所以我想可以通过重写DataGrid的Render方法来实现多层表头的显示,代码如下:
using
System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace CustomDBGrid
{
/**//**//**//**//**//**//**//**//**//**//**//**//**//**//**//// <summary>
/// Custom DataGrid 的摘要说明。
/// </summary>
[ToolboxData("<{0}:CustomDataGrid runat=server></{0}:CustomDataGrid>")]
public class CustomDataGrid : System.Web.UI.WebControls.DataGrid
{
public string HeadHTML
{
get{
if(ViewState["HeadHTML"] != null)
return ViewState["HeadHTML"].ToString();
else
return null;
}
set{
ViewState["HeadHTML"] = value;
}
}
protected override void Render(HtmlTextWriter writer)
{
System.IO.StringWriter stringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter Tem_Writer = new HtmlTextWriter(stringWriter);
//获得原始输出结果
base.Render(Tem_Writer);
string Tem_Str = stringWriter.ToString();
string StrToRender = Tem_Str;
if(this.HeadHTML != null)
{
//得到默认的表格第一行的HTML,这一行就是表格头了
int Index_Head = Tem_Str.IndexOf("<tr>");
int Index_Last = Tem_Str.IndexOf("</tr>");
string OldHeadHTML = Tem_Str.Substring(Index_Head,Index_Last+5-Index_Head);
//将默认的表格头替换成我们想要的表格头HTML
StrToRender = Tem_Str.Replace(OldHeadHTML,this.HeadHTML);
}
//将新的结果输出到writer
writer.Write(StrToRender);
}
}
}
在有需要使用多层表头的地方使用这个自定义的DataGrid,只需要在DreamWeaver里面设计好一个表头,然后把CustomDatagrid的HeadHTML属性设置为在DreamWeaver中生成的HTML即可。using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace CustomDBGrid
{
/**//**//**//**//**//**//**//**//**//**//**//**//**//**//**//// <summary>
/// Custom DataGrid 的摘要说明。
/// </summary>
[ToolboxData("<{0}:CustomDataGrid runat=server></{0}:CustomDataGrid>")]
public class CustomDataGrid : System.Web.UI.WebControls.DataGrid
{
public string HeadHTML
{
get{
if(ViewState["HeadHTML"] != null)
return ViewState["HeadHTML"].ToString();
else
return null;
}
set{
ViewState["HeadHTML"] = value;
}
}
protected override void Render(HtmlTextWriter writer)
{
System.IO.StringWriter stringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter Tem_Writer = new HtmlTextWriter(stringWriter);
//获得原始输出结果
base.Render(Tem_Writer);
string Tem_Str = stringWriter.ToString();
string StrToRender = Tem_Str;
if(this.HeadHTML != null)
{
//得到默认的表格第一行的HTML,这一行就是表格头了
int Index_Head = Tem_Str.IndexOf("<tr>");
int Index_Last = Tem_Str.IndexOf("</tr>");
string OldHeadHTML = Tem_Str.Substring(Index_Head,Index_Last+5-Index_Head);
//将默认的表格头替换成我们想要的表格头HTML
StrToRender = Tem_Str.Replace(OldHeadHTML,this.HeadHTML);
}
//将新的结果输出到writer
writer.Write(StrToRender);
}
}
}
比如你在DreamWeaver中得到的表头HTML是:
<
TABLE
>
< TR >
< TD rowspan ="2" >< div align ="center" ></ div >
< div align ="center" > Head1 </ div ></ TD >
< TD rowspan ="2" >< div align ="center" ></ div >
< div align ="center" > Head2 </ div ></ TD >
< TD colspan ="2" >< div align ="center" > Head3 </ div ></ TD >
< TD rowspan ="2" >< div align ="center" ></ div >
< div align ="center" > Head4 </ div ></ TD >
</ TR >
< TR >
< TD >< div align ="center" > SubTitle1 </ div ></ TD >
< TD >< div align ="center" > SubTitle2 </ div ></ TD >
</ TR >
</ TABLE >
那么你只需把下面这段字符串赋值给CustomDataGrid的HeadHTML:< TR >
< TD rowspan ="2" >< div align ="center" ></ div >
< div align ="center" > Head1 </ div ></ TD >
< TD rowspan ="2" >< div align ="center" ></ div >
< div align ="center" > Head2 </ div ></ TD >
< TD colspan ="2" >< div align ="center" > Head3 </ div ></ TD >
< TD rowspan ="2" >< div align ="center" ></ div >
< div align ="center" > Head4 </ div ></ TD >
</ TR >
< TR >
< TD >< div align ="center" > SubTitle1 </ div ></ TD >
< TD >< div align ="center" > SubTitle2 </ div ></ TD >
</ TR >
</ TABLE >
<
TR
>
< TD rowspan ="2" >< div align ="center" ></ div >
< div align ="center" > Head1 </ div ></ TD >
< TD rowspan ="2" >< div align ="center" ></ div >
< div align ="center" > Head2 </ div ></ TD >
< TD colspan ="2" >< div align ="center" > Head3 </ div ></ TD >
< TD rowspan ="2" >< div align ="center" ></ div >
< div align ="center" > Head4 </ div ></ TD >
</ TR >
< TR >
< TD >< div align ="center" > SubTitle1 </ div ></ TD >
< TD >< div align ="center" > SubTitle2 </ div ></ TD >
</ TR >
< TD rowspan ="2" >< div align ="center" ></ div >
< div align ="center" > Head1 </ div ></ TD >
< TD rowspan ="2" >< div align ="center" ></ div >
< div align ="center" > Head2 </ div ></ TD >
< TD colspan ="2" >< div align ="center" > Head3 </ div ></ TD >
< TD rowspan ="2" >< div align ="center" ></ div >
< div align ="center" > Head4 </ div ></ TD >
</ TR >
< TR >
< TD >< div align ="center" > SubTitle1 </ div ></ TD >
< TD >< div align ="center" > SubTitle2 </ div ></ TD >
</ TR >
好了,现在不管在设计试图和还是在运行页面都可以看到多层表头的样子了。