GridView (固定表头,合并表头)
请大侠帮忙看看问题,感激不尽:(要求表头三行都要固定,可是只有第一行固定。)
显示效果图:
![](https://images.cnblogs.com/cnblogs_com/luqioudi/1.jpg)
![](https://images.cnblogs.com/cnblogs_com/luqioudi/2.jpg)
页面代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
<head runat="server">
<title>未命名頁面</title>
<style type="text/css">
.Freezing
{
position:relative ;
table-layout:fixed;
top:expression(this.offsetParent.scrollTop);
z-index: 10;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div style="overflow-y: scroll; height: 200px;width:200px" >
<asp:GridView ID="GridView1" runat="server" OnRowCreated="GridView1_RowCreated1" OnRowDataBound="GridView1_RowDataBound">
<HeaderStyle CssClass="Freezing" />
</asp:GridView>
</div>
</form>
</body>这是后台代码
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.GridView1.DataSource = GetDT();
this.GridView1.DataBind();
}
}
private DataTable GetDT()
{
DataTable dt = new DataTable();
DataColumn dc = new DataColumn("編號", typeof(Int32));
dt.Columns.Add(dc);
DataColumn dc2 = new DataColumn("姓名",typeof(string));
dt.Columns.Add(dc2);
DataColumn dc3 = new DataColumn("年齡", typeof(Int32));
dt.Columns.Add(dc3);
DataColumn dc4 = new DataColumn("語文",typeof(Int32));
dt.Columns.Add(dc4);
DataColumn dc5 = new DataColumn("數學",typeof(Int32));
dt.Columns.Add(dc5);
for (int i = 50; i < 100; i++)
{
DataRow dr = dt.NewRow();
dr["編號"] = i;
dr["姓名"] = "姓名"+i.ToString();
dr["年齡"] = i;
dr["語文"] = i;
dr["數學"] = i;
dt.Rows.Add(dr);
}
return dt;
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
}
protected void GridView1_RowCreated1(object sender, GridViewRowEventArgs e)
{
switch(e.Row.RowType)
{
case DataControlRowType.Header:
TableCellCollection tcHeader = e.Row.Cells;
tcHeader.Clear();
//第一行
tcHeader.Add(new TableHeaderCell());
tcHeader[0].Attributes.Add("colspan","5");
tcHeader[0].Attributes.Add("bgcolor","skyBlue");
tcHeader[0].Text = "全部信息</th></tr><tr>";
//第二行
tcHeader.Add(new TableHeaderCell());
tcHeader[1].Attributes.Add("bgcolor", "red");
tcHeader[1].Attributes.Add("colspan", "2");
tcHeader[1].Text = "基本信息";
tcHeader.Add(new TableHeaderCell());
tcHeader[2].Attributes.Add("bgcolor","red");
tcHeader[2].Attributes.Add("colspan","3");
tcHeader[2].Text = "課程信息</th></tr><tr>";
//第三行
tcHeader.Add(new TableHeaderCell());
tcHeader[3].Attributes.Add("bgcolor","DarkSeaGreen");
tcHeader[3].Attributes.Add("colspan","1");
tcHeader[3].Text = "編號";
tcHeader.Add(new TableHeaderCell());
tcHeader[4].Attributes.Add("bgcolor","DarkSeaGreen");
tcHeader[4].Attributes.Add("colspan","1");
tcHeader[4].Text = "姓名";
tcHeader.Add(new TableHeaderCell());
tcHeader[5].Attributes.Add("bgcolor", "DarkSeaGreen");
tcHeader[5].Attributes.Add("colspan", "1");
tcHeader[5].Text = "語文";
tcHeader.Add(new TableHeaderCell());
tcHeader[6].Attributes.Add("bgcolor", "DarkSeaGreen");
tcHeader[6].Attributes.Add("colspan", "1");
tcHeader[6].Text = "數學";
tcHeader.Add(new TableHeaderCell());
tcHeader[7].Attributes.Add("bgcolor", "DarkSeaGreen");
tcHeader[7].Attributes.Add("colspan", "1");
tcHeader[7].Text = "外語";
break;
}
}
显示效果图:
![](https://images.cnblogs.com/cnblogs_com/luqioudi/1.jpg)
![](https://images.cnblogs.com/cnblogs_com/luqioudi/2.jpg)
页面代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<head runat="server">
<title>未命名頁面</title>
<style type="text/css">
.Freezing
{
position:relative ;
table-layout:fixed;
top:expression(this.offsetParent.scrollTop);
z-index: 10;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div style="overflow-y: scroll; height: 200px;width:200px" >
<asp:GridView ID="GridView1" runat="server" OnRowCreated="GridView1_RowCreated1" OnRowDataBound="GridView1_RowDataBound">
<HeaderStyle CssClass="Freezing" />
</asp:GridView>
</div>
</form>
</body>
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.GridView1.DataSource = GetDT();
this.GridView1.DataBind();
}
}
private DataTable GetDT()
{
DataTable dt = new DataTable();
DataColumn dc = new DataColumn("編號", typeof(Int32));
dt.Columns.Add(dc);
DataColumn dc2 = new DataColumn("姓名",typeof(string));
dt.Columns.Add(dc2);
DataColumn dc3 = new DataColumn("年齡", typeof(Int32));
dt.Columns.Add(dc3);
DataColumn dc4 = new DataColumn("語文",typeof(Int32));
dt.Columns.Add(dc4);
DataColumn dc5 = new DataColumn("數學",typeof(Int32));
dt.Columns.Add(dc5);
for (int i = 50; i < 100; i++)
{
DataRow dr = dt.NewRow();
dr["編號"] = i;
dr["姓名"] = "姓名"+i.ToString();
dr["年齡"] = i;
dr["語文"] = i;
dr["數學"] = i;
dt.Rows.Add(dr);
}
return dt;
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
}
protected void GridView1_RowCreated1(object sender, GridViewRowEventArgs e)
{
switch(e.Row.RowType)
{
case DataControlRowType.Header:
TableCellCollection tcHeader = e.Row.Cells;
tcHeader.Clear();
//第一行
tcHeader.Add(new TableHeaderCell());
tcHeader[0].Attributes.Add("colspan","5");
tcHeader[0].Attributes.Add("bgcolor","skyBlue");
tcHeader[0].Text = "全部信息</th></tr><tr>";
//第二行
tcHeader.Add(new TableHeaderCell());
tcHeader[1].Attributes.Add("bgcolor", "red");
tcHeader[1].Attributes.Add("colspan", "2");
tcHeader[1].Text = "基本信息";
tcHeader.Add(new TableHeaderCell());
tcHeader[2].Attributes.Add("bgcolor","red");
tcHeader[2].Attributes.Add("colspan","3");
tcHeader[2].Text = "課程信息</th></tr><tr>";
//第三行
tcHeader.Add(new TableHeaderCell());
tcHeader[3].Attributes.Add("bgcolor","DarkSeaGreen");
tcHeader[3].Attributes.Add("colspan","1");
tcHeader[3].Text = "編號";
tcHeader.Add(new TableHeaderCell());
tcHeader[4].Attributes.Add("bgcolor","DarkSeaGreen");
tcHeader[4].Attributes.Add("colspan","1");
tcHeader[4].Text = "姓名";
tcHeader.Add(new TableHeaderCell());
tcHeader[5].Attributes.Add("bgcolor", "DarkSeaGreen");
tcHeader[5].Attributes.Add("colspan", "1");
tcHeader[5].Text = "語文";
tcHeader.Add(new TableHeaderCell());
tcHeader[6].Attributes.Add("bgcolor", "DarkSeaGreen");
tcHeader[6].Attributes.Add("colspan", "1");
tcHeader[6].Text = "數學";
tcHeader.Add(new TableHeaderCell());
tcHeader[7].Attributes.Add("bgcolor", "DarkSeaGreen");
tcHeader[7].Attributes.Add("colspan", "1");
tcHeader[7].Text = "外語";
break;
}
}