- <%@ 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 id="Head1" runat="server">
- <title>GridView复合表头</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="10" ForeColor="#333333" PageSize="3" OnRowCreated="GridView1_RowCreated">
- <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
- <Columns>
- <asp:BoundField DataField="ID" HeaderText="用户ID" ReadOnly="True" />
- <asp:BoundField DataField="Name" HeaderText="用户姓名" />
- <asp:BoundField DataField="Sex" HeaderText="性别" />
- <asp:BoundField DataField="Birthday" HeaderText="出生日期" DataFormatString="{0:yyyy年M月dd日}" HtmlEncode="false" />
- <asp:BoundField DataField="Salary" HeaderText="薪水" />
- <asp:BoundField DataField="Address" HeaderText="家庭住址" />
- <asp:BoundField DataField="PostCode" HeaderText="邮政编码" />
- </Columns>
- <RowStyle ForeColor="#000066" />
- <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
- <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
- <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
- </asp:GridView>
- </div>
- </form>
- </body>
- </html>
- using System;
- using System.Data;
- using System.Data.SqlClient;
- 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;
- public partial class _Default : System.Web.UI.Page
- {
- SqlConnection sqlConn;
- SqlCommand sqlComm;
- string strConn = "Data Source=(local);Database=Exercise;Uid=sa;Pwd=sa";
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- databind();
- }
- }
- private void databind()
- {
- string strSql = "select * from myDt";
- sqlConn = new SqlConnection(strConn);
- SqlDataAdapter sqlDa = new SqlDataAdapter(strSql, sqlConn);
- DataSet ds = new DataSet();
- sqlConn.Open();
- sqlDa.Fill(ds, "myDt");
- GridView1.DataSource = ds;
- GridView1.DataKeyNames = new string[] { "ID" };
- GridView1.DataBind();
- sqlConn.Close();
- }
- protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
- {
- switch (e.Row.RowType)
- {
- case DataControlRowType.DataRow:
- break;
- case DataControlRowType.EmptyDataRow:
- break;
- case DataControlRowType.Footer:
- break;
- case DataControlRowType.Header:
- //第一行表头
- TableCellCollection tcHeader = e.Row.Cells;
- tcHeader.Clear();
- tcHeader.Add(new TableHeaderCell());
- tcHeader[0].Attributes.Add("colspan", "7"); //跨Column
- tcHeader[0].Text = "全部信息</th></tr><tr>";
- //第二行表头
- tcHeader.Add(new TableHeaderCell());
- tcHeader[1].Attributes.Add("bgcolor", "DarkSeaGreen");
- tcHeader[1].Text = "用户ID";
- tcHeader.Add(new TableHeaderCell());
- tcHeader[2].Attributes.Add("bgcolor", "LightSteelBlue");
- tcHeader[2].Attributes.Add("colspan", "3");
- tcHeader[2].Text = "基本信息";
- tcHeader.Add(new TableHeaderCell());
- tcHeader[3].Attributes.Add("bgcolor", "DarkSeaGreen");
- tcHeader[3].Text = "福利";
- tcHeader.Add(new TableHeaderCell());
- tcHeader[4].Attributes.Add("bgcolor", "LightSteelBlue");
- tcHeader[4].Attributes.Add("colspan", "2");
- tcHeader[4].Text = "联系方式</th></tr><tr>";
- //第三行表头
- tcHeader.Add(new TableHeaderCell());
- tcHeader[5].Attributes.Add("bgcolor", "Khaki");
- tcHeader[5].Text = "用户ID";
- tcHeader.Add(new TableHeaderCell());
- tcHeader[6].Attributes.Add("bgcolor", "Khaki");
- tcHeader[6].Text = "姓名";
- tcHeader.Add(new TableHeaderCell());
- tcHeader[7].Attributes.Add("bgcolor", "Khaki");
- tcHeader[7].Text = "性别";
- tcHeader.Add(new TableHeaderCell());
- tcHeader[8].Attributes.Add("bgcolor", "Khaki");
- tcHeader[8].Text = "出生日期";
- tcHeader.Add(new TableHeaderCell());
- tcHeader[9].Attributes.Add("bgcolor", "Khaki");
- tcHeader[9].Text = "薪水";
- tcHeader.Add(new TableHeaderCell());
- tcHeader[10].Attributes.Add("bgcolor", "Khaki");
- tcHeader[10].Text = "家庭住址";
- tcHeader.Add(new TableHeaderCell());
- tcHeader[11].Attributes.Add("bgcolor", "Khaki");
- tcHeader[11].Text = "邮政编码";
- break;
- case DataControlRowType.Pager:
- break;
- case DataControlRowType.Separator:
- break;
- default:
- break;
- }
- }
- }