.NET GridView 多层表头

.NET GridView 多层表头实现方法

效果如图:

.NET GridView 多层表头 - BEYONDER - BEYONDER

 

后台代码文件

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using BEYOND;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
  BS bs = new BS();
  gvBS.DataSource = bs.DataTableBS;
  gvBS.DataBind();

    }

 protected void gvBS_RowCreated(object sender, GridViewRowEventArgs e)
 {
  if (e.Row.RowType == DataControlRowType.Header)
  {
   #region-----一层表头-----
   GridViewRow rh0 = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
   rh0.BackColor = System.Drawing.Color.FromArgb(51, 153, 255);
   rh0.ForeColor = System.Drawing.Color.FromArgb(255, 255, 255);
   rh0.Font.Bold = true;

   TableCellCollection c0 = e.Row.Cells;

   TableCell hc = new TableCell();
   hc.Text = "BS";
   hc.RowSpan = 2;
   rh0.Cells.Add(hc);
   hc.HorizontalAlign = HorizontalAlign.Center;
   rh0.Cells.Add(hc);

   hc = new TableCell();
   hc.Text = "BEYOND";
   hc.ColumnSpan = 4;
   hc.HorizontalAlign = HorizontalAlign.Center;
   rh0.Cells.Add(hc);

   hc = new TableCell();
   hc.Text = "羽泉";
   hc.ColumnSpan = 2;
   rh0.Cells.Add(hc);
   hc.HorizontalAlign = HorizontalAlign.Center;

   hc = new TableCell();
   hc.Text = "梦想";
   hc.RowSpan = 2;
   rh0.Cells.Add(hc);
   hc.HorizontalAlign = HorizontalAlign.Center;

   rh0.Visible = true;
   gvBS.Controls[0].Controls.AddAt(0, rh0);
   #endregion

   #region-----二层表头-----
   GridViewRow rh1 = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
   rh1.BackColor = System.Drawing.Color.FromArgb(51, 153, 255);
   rh1.ForeColor = System.Drawing.Color.FromArgb(255, 255, 255);
   rh1.Font.Bold = true;

   TableCellCollection c1 = e.Row.Cells;

   TableCell hc1 = new TableCell();
   hc1.Text = "黄家驹";
   hc1.RowSpan = 1;
   rh1.Cells.Add(hc1);
   hc1.HorizontalAlign = HorizontalAlign.Center;
   rh1.Cells.Add(hc1);

   hc1 = new TableCell();
   hc1.Text = "黄贯中";
   hc1.RowSpan = 1;
   hc1.HorizontalAlign = HorizontalAlign.Center;
   rh1.Cells.Add(hc1);

   hc1 = new TableCell();
   hc1.Text = "黄家强";
   hc1.RowSpan = 1;
   hc1.HorizontalAlign = HorizontalAlign.Center;
   rh1.Cells.Add(hc1);

   hc1 = new TableCell();
   hc1.Text = "叶世荣";
   hc1.RowSpan = 1;
   rh1.Cells.Add(hc1);
   hc1.HorizontalAlign = HorizontalAlign.Center;

   hc1 = new TableCell();
   hc1.Text = "陈羽凡";
   hc1.ColumnSpan = 1;
   hc1.HorizontalAlign = HorizontalAlign.Center;
   rh1.Cells.Add(hc1);

   hc1 = new TableCell();
   hc1.Text = "胡海泉";
   hc1.ColumnSpan = 1;
   hc1.HorizontalAlign = HorizontalAlign.Center;
   rh1.Cells.Add(hc1);

   rh1.Visible = true;
   gvBS.Controls[0].Controls.AddAt(1, rh1);
   #endregion
  }
 }

}

  数据表类

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace BEYOND
{
 /// <summary>
 /// DataTableBS の概要の説明です
 /// </summary>
 public class BS
 {
  private DataTable dtBS;

  public DataTable DataTableBS
  {
   get { return dtBS; }
   set { dtBS = value; }
  }

  public BS()
  {
   //
   // TODO: コンストラクタ ロジックをここに追加します
   //

   int len = 8;
   DataRow dr;
   string[] sData;

   dtBS = new DataTable();
   dtBS.Columns.Add("BS");
   dtBS.Columns.Add("BEYOND01");
   dtBS.Columns.Add("BEYOND02");
   dtBS.Columns.Add("BEYOND03");
   dtBS.Columns.Add("BEYOND04");
   dtBS.Columns.Add("YuQuan01");
   dtBS.Columns.Add("YuQuan02");
   dtBS.Columns.Add("Dream");

   sData = new string[len];
   sData[0] = "海贼王";
   sData[1] = "长城";
   sData[2] = "大地";
   sData[3] = "冷雨夜";
   sData[4] = "午夜怨曲";
   sData[5] = "惩罚";
   sData[6] = "种子";
   sData[7] = "和平";

   dr = dtBS.NewRow();
   for (int i = 0; i < len; i++)
   {
    dr[i] = sData[i];
   }

   dtBS.Rows.Add(dr);
  }
 }
}

 

前台源码文件

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>多层表头</title>
</head>
<body style ="text-align :center">
    <form id="form1" runat="server">
    <div>
        <table cellpadding = "0" cellspacing = "0" border= "1">
            <tr>
                <td align = "center">
        <asp:GridView ID="gvBS" runat="server" AutoGenerateColumns="False" OnRowCreated="gvBS_RowCreated" ShowHeader="False">
            <Columns>
                <asp:BoundField DataField="BS" HeaderText="BS" />
                <asp:BoundField DataField="BEYOND01" HeaderText="BEYOND01" />
                <asp:BoundField DataField="BEYOND02" HeaderText="BEYOND02" />
                <asp:BoundField DataField="BEYOND03" HeaderText="BEYOND03" />
                <asp:BoundField DataField="BEYOND04" HeaderText="BEYOND04" />
                <asp:BoundField DataField="YuQuan01" HeaderText="YuQuan01" />
                <asp:BoundField DataField="YuQuan02" HeaderText="YuQuan02" />
                <asp:BoundField DataField="Dream" HeaderText="Dream" />
            </Columns>
        </asp:GridView>
                </td>
            </tr>
        </table>
   
    </div>
    </form>
</body>
</html>
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值