<div class="codetitle"> <span><a style="CURSOR: pointer" data="87618" class="copybut" id="copybut87618" onclick="doCopy('code87618')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code87618"> <br> <br><br> <br><br><script runat="server"> <BR>// 计算数据,完全可以从数据看取得 <BR>ICollection CreateDataSource( ) <BR>{ <BR>System.Data.DataTable dt = new System.Data.DataTable(); <BR>System.Data.DataRow dr; <BR>dt.Columns.Add(new System.Data.DataColumn("学生班级", typeof(System.String))); <BR>dt.Columns.Add(new System.Data.DataColumn("学生姓名", typeof(System.String))); <BR>dt.Columns.Add(new System.Data.DataColumn("语文", typeof(System.Decimal))); <BR>dt.Columns.Add(new System.Data.DataColumn("数学", typeof(System.Decimal))); <BR>dt.Columns.Add(new System.Data.DataColumn("英语", typeof(System.Decimal))); <BR>dt.Columns.Add(new System.Data.DataColumn("计算机", typeof(System.Decimal))); <br><br>for (int i = 0; i < 50; i++) <BR>{ <BR>System.Random rd = new System.Random(Environment.TickCount * i); ; <BR>dr = dt.NewRow(); <BR>dr[0] = "班级" + i.ToString(); <BR>dr[1] = "测试" + i.ToString(); <BR>dr[2] = System.Math.Round(rd.NextDouble() * 100, 2); <BR>dr[3] = System.Math.Round(rd.NextDouble() * 100, 2); <BR>dr[4] = System.Math.Round(rd.NextDouble() * 100, 2); <BR>dr[5] = System.Math.Round(rd.NextDouble() * 100, 2); <BR>dt.Rows.Add(dr); <BR>} <BR>System.Data.DataView dv = new System.Data.DataView(dt); <BR>return dv; <BR>} <br><br>protected void Page_Load( object sender, EventArgs e ) <BR>{ <BR>if (!IsPostBack) <BR>{ <BR>GridView1.Attributes.Add("style", "table-layout:fixed"); <BR>GridView1.DataSource = CreateDataSource(); <BR>GridView1.DataBind(); <BR>} <BR>} <br><br></script> <br><br><script type="text/javascript"> <BR>function s() { <BR>var t = document.getElementById("<%=GridView1.ClientID%>"); <BR>var t2 = t.cloneNode(true) <BR>for (i = t2.rows.length - 1; i > 0; i--) <BR>t2.deleteRow(i) <BR>t.deleteRow(0) <BR>a.appendChild(t2) <BR>} <BR>window.onload = s <BR></script> <br><br> <br> <br><title>创建表头固定,表体可滚动的GridView</title> <br> <br> <br><form id="Form1" runat="server"> <br><table> <br><tr> <br><td> <br><div id="a"> <br> </div> <br><div style="overflow-y: scroll; height: 200px"> <br><gridview id="GridView1" runat="server" font-size="12px" backcolor="#FFFFFF">GridLines="Both" CellPadding="4" Width="560"> <br><headerstyle backcolor="#EDEDED" height="26px"></headerstyle> <br></gridview> <br> </div> <br> </td> <br> </tr> <br> </table> <br> </form> <br> <br> <br> </div>