DataGrid中创建复杂表头(转载)

方法一:用table实现

  <form id="Form1" method="post" runat="server">
   <TABLE id="Table1" runat="server" cellSpacing="1" cellPadding="2" width="580" border="1"
    bgcolor="#cc6633" bordercolor="#cc9966" style="FONT-SIZE: 9pt;BORDER-BOTTOM:0px">
    <TR align="center">
     <TD colspan="2" width="380" style="HEIGHT: 21px"></TD>
     <TD width="200" colspan="2" style="HEIGHT: 21px"></TD>
    </TR>
    <TR align="center">
     <TD width="200" bgcolor="#66cc99"><FONT face="
宋体"></FONT></TD>
     <TD width="180" bgcolor="white"><FONT face="
宋体"></FONT></TD>
     <TD width="160" bgcolor="#99cccc"></TD>
     <TD width="40" bgcolor="#009999"></TD>
    </TR>
   </TABLE>
   <asp:DataGrid id="DataGrid1" width="580px" AlternatingItemStyle-BackColor="#6699ff" CellPadding="2"
    CellSpacing="1" BorderWidth="1" BorderColor="#cc9966" Font-Size=" 9pt" runat="server" ShowHeader="False"
    AutoGenerateColumns="False">
    <Columns>
     <asp:BoundColumn DataField="Title">
      <ItemStyle Width="200px"></ItemStyle>
     </asp:BoundColumn>
     <asp:BoundColumn DataField="CreateDate">
      <ItemStyle Width="180px"></ItemStyle>
     </asp:BoundColumn>
     <asp:BoundColumn DataField="pid">
      <ItemStyle Width="160px"></ItemStyle>
     </asp:BoundColumn>
     <asp:BoundColumn DataField="HitCount">
      <ItemStyle Width="40px"></ItemStyle>
     </asp:BoundColumn>
    </Columns>
   </asp:DataGrid>   
  </form> 

 

窗体顶端

 

 

 

 

 

 

 

 

 

 

 

 

    

窗体底端

方法二:动态生成表头

生成双层表头:
  private void grid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
  {

   if (e.Item.ItemType == ListItemType.Header)
   {
//    e.Item.Cells[0].ColumnSpan = 1;//
这是第一列的跨列数
    StringBuilder strtext=new StringBuilder();
    strtext.Append("//</td>");
    strtext.Append("<td colspan=4>
生活照明</td>");
    strtext.Append("<td colspan=2>
一般照明</td>");
    strtext.Append("<td colspan=2>
工付业</td>");
    strtext.Append("<td colspan=2>
农业</td>");
    strtext.Append("<td colspan=2>
合计</td>");
    strtext.Append("</tr>");
    strtext.Append("<tr>");
    strtext.Append("<td>" + e.Item.Cells[0].Text);               
    e.Item.Cells[0].Text =strtext.ToString();

   }

}

整个表头内容:<tr><td>  e.Item.Cells[0].Text =的内容  </td></tr>

加起来就是表头的样式。

生活照明

 

 

一般照明

 

 

工付业

 

 

农业

 

 

合计

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

            

生成三层表头:

if (e.Item.ItemType == ListItemType.Header)
   {    
    StringBuilder strtext=new StringBuilder();
    strtext.Append("</td>");
    strtext.Append("<td colspan= 6>
当月</td>");
    strtext.Append("</tr>"); 
   strtext.Append("<tr>"); 
    strtext.Append("<td colspan=2>
居民</td>");
    strtext.Append("<td colspan=2>
一般</td>");
    strtext.Append("<td colspan=2>
工付业</td>");
    strtext.Append("</tr>"); 
    strtext.Append("<tr>");
    strtext.Append("<td>" + e.Item.Cells[0].Text);               
    e.Item.Cells[0].Text =strtext.ToString();
   } 
 

当月

 

 

居民

 

 

一般

 

 

工付业

 

 

 

 

 

 

 

 

 

 

 

 

 

 

      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值