关于gridview中head固定的方法目前Tony小编共整理的两种方法,供大家借鉴参考(代码源于网络),需要在此说明的是,方法一用CSS样式来控制gridview中的head固定的方法,效果只能应用于IE浏览器, 方法二可以较好的应用于各种浏览器。
方法一:[CSS]
前台代码:
<head runat="server">
<title>GridView固定表头</title>
<style type="text/css">
.staicTitlteDiv
{
WIDTH: 1000px;
height:500px ;
OVERFLOW: scroll;
SCROLLBAR-FACE-COLOR:EAECEC;
SCROLLBAR-HIGHLIGHT-COLOR: EAECEC;
SCROLLBAR-SHADOW-COLOR: BLACK;
SCROLLBAR-3DLIGHT-COLOR: EAECEC;
SCROLLBAR-ARROW-COLOR: EAECEC;
SCROLLBAR-TRACK-COLOR: FFFFFF;
SCROLLBAR-DARKSHADOW-COLOR: EAECEC;
}
.fixedHeaderTr
{
position:relative;
border:0;
top:expression(this.offsetParent.scrollTop);
}
th
{
position:relative;
background-color: Beige;
top:expression(this.offsetParent.scrollTop);
}
</style>
</head>
<body style="text-align:center">
<form id="form1" runat="server" >
<div class="staicTitlteDiv">
<asp:GridView ID="GridView1"runat="server">
<HeaderStyleCssClass="fixedHeaderTr" BorderColor="Beige" />
</asp:GridView>
</div>
</form>
</body>
后台代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.RowDataBound += new GridViewRowEventHandler(GridView1_RowDataBound);
GridView1.DataBound += new EventHandler(GridView_DataBound);
GridView1DataBind();
}
}
void GridView1DataBind()
{
DataTable table = new DataTable();
table.Columns.Add("class", typeof(string)).SetOrdinal(0);
table.Columns.Add("student", typeof(string)).SetOrdinal(1);
table.Columns.Add("Languages", typeof(Double)).SetOrdinal(2);
table.Columns.Add("Rate1", typeof(Double)).SetOrdinal(3);
table.Columns.Add("Mathematics", typeof(Double)).SetOrdinal(4);
table.Columns.Add("Rate2", typeof(Double)).SetOrdinal(5);
table.Columns.Add("English", typeof(Double)).SetOrdinal(6);
table.Columns.Add("Rate3", typeof(Double)).SetOrdinal(7);
table.Columns.Add("Other", typeof(Double)).SetOrdinal(8);
table.Columns.Add("TotalScore", typeof(Double), "ISNULL(Languages,0)+ISNULL(Mathematics,0)+ISNULL(English,0)+ISNULL(Other,0)").SetOrdinal(9);
table.Columns[3].Expression = "Languages/TotalScore";
table.Columns[5].Expression = "Mathematics/TotalScore";
table.Columns[7].Expression = "English/TotalScore";
Random random = new Random();
for (int i = 0; i < 100; i++)
{
DataRow row = table.NewRow();
row["student"] = "学生" + i;
row["class"] = "班级" + random.Next(0, 10);
row["Languages"] = random.Next(0, 151);
row["Mathematics"] = random.Next(0, 151);
row["English"] = random.Next(0, 151);
row["Other"] = random.Next(0, 150 * 3 + 1);
table.Rows.Add(row);
}
//GridView1.DataSource =GridViewGroup.AddGurop(table, "class");
GridView1.DataSource = GridViewGroup.AddGurop(table, "class", "Languages#Mathematics#English#Other");
GridView1.DataBind();
}
void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
string header = "班级#学生#其中 语文 成绩,比例#其中 数学 成绩,比例#其中 英语 成绩,比例#其中 其他3科#总成绩";
AndyGridViewTHeaderHepler help = new AndyGridViewTHeaderHepler();
e.Row.CssClass = "fixedHeaderTr";
e.Row.BackColor = System.Drawing.Color.Beige;
help.SplitTableHeader(e.Row, header);
}
else
{
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='White'");
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor=\"" + "#E7E7FF" + "\"");
}
}
protected void GridView_DataBound(object sender, EventArgs e)
{
int index = -1;
int startGroupIndex = 0;
foreach (GridViewRow row in GridView1.Rows)
{
index++;
if (index > 0 && (row.Cells[0].Text.ToString().Trim() == "" || row.Cells[0].Text.ToString().Trim() == " "))
{
row.Cells[0].Text = "小计";// GridView1.Rows[startGroupIndex].Cells[0].Text;
row.Attributes.Add("style", "background-color: #F2F2F2; font-weight: bold;font-variant: normal; color: #000000;text-align:center;");
row.Attributes.Remove("onmouseout");
row.Attributes.Remove("onmouseover");
GridViewGroup.GroupRow(GridView1, index, 0, 2);
GridViewGroup.GropCell(GridView1, 0, startGroupIndex, index - 1);
startGroupIndex = index + 1;
}
}
GridViewRow sumRow = GridView1.Rows[index];
sumRow.Attributes.Add("style", "background-color: #EAEAEA;font-weight: bold;font-variant: normal;color: #000000;text-align: center;");
sumRow.Attributes.Remove("onmouseout");
sumRow.Attributes.Remove("onmouseover");
GridViewGroup.GroupRow(GridView1, index, 0, 2);
}
方法二:[JS]
前台代码:
<script type="text/javascript">
function s() {
var t = document.getElementById("<%=GridView1.ClientID%>");
var t2 = t.cloneNode(true)
for (i = t2.rows.length - 1; i > 0; i--)
t2.deleteRow(i)
t.deleteRow(0)
a.appendChild(t2)
}
window.onload = s
</script>
<head>
<title>创建表头固定,表体可滚动的GridView</title>
</head>
<body>
<form id="Form1" runat="server">
<table>
<tr>
<td>
<div id="a">
</div>
<div style="overflow-y: scroll; height: 200px">
<asp:GridView ID="GridView1" runat="server" Font-Size="12px" BackColor="#FFFFFF"
GridLines="Both" CellPadding="4" Width="100%">
<HeaderStyle BackColor="#EDEDED" Height="26px" />
</asp:GridView>
</div>
</td>
</tr>
</table>
</form>
</body>
后台代码:
protected void Page_Load(object sender, EventArgs e)
{
GridView1.Attributes.Add("style", "table-layout:fixed");
GridView1.DataSource = CreateDataSource();
GridView1.DataBind();
}