日期:2014-05-18 浏览次数:20444 次
<%@ Page Language="C#" EnableViewState="false" %> <script runat="server"> // 计算数据,完全可以从数据看取得 ICollection CreateDataSourceByXianhuiMeng() { System.Data.DataTable dt = new System.Data.DataTable(); System.Data.DataRow dr; dt.Columns.Add(new System.Data.DataColumn("学生班级", typeof(System.String))); dt.Columns.Add(new System.Data.DataColumn("学生姓名", typeof(System.String))); dt.Columns.Add(new System.Data.DataColumn("语文", typeof(System.Decimal))); dt.Columns.Add(new System.Data.DataColumn("数学", typeof(System.Decimal))); dt.Columns.Add(new System.Data.DataColumn("英语", typeof(System.Decimal))); dt.Columns.Add(new System.Data.DataColumn("计算机", typeof(System.Decimal))); for (int i = 0; i < 20; i++) { System.Random rd = new System.Random(Environment.TickCount * i); ; dr = dt.NewRow(); dr[0] = "班级" + i.ToString(); dr[1] = "【孟子E章】" + i.ToString(); dr[2] = System.Math.Round(rd.NextDouble() * 100, 2); dr[3] = System.Math.Round(rd.NextDouble() * 100, 2); dr[4] = System.Math.Round(rd.NextDouble() * 100, 2); dr[5] = System.Math.Round(rd.NextDouble() * 100, 2); dt.Rows.Add(dr); } System.Data.DataView dv = new System.Data.DataView(dt); return dv; } protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { GridView1.DataSource = CreateDataSourceByXianhuiMeng(); GridView1.DataBind(); } } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <script type="text/javascript"> var tblID = "<%=GridView1.ClientID %>"; function lockCol() { var table = document.getElementById(tblID); var cTR = table.getElementsByTagName('TR'); //collection of rows if (table.rows[0].cells[0].className == '') { for (i = 0; i < cTR.length; i++) { var tr = cTR.item(i); tr.cells[0].className = 'locked' } } else { for (i = 0; i < cTR.length; i++) { var tr = cTR.item(i); tr.cells[0].className = '' } } } function createHeadBody() { var t = document.getElementById(tblID); var h = t.createTHead() h.appendChild(t.tBodies[0].firstChild) t.parentNode.id = "tbl-container"; lockCol() } window.onload = createHeadBody; </script> <style type="text/css"> div#tbl-container { width: 418px; height: 252px; overflow: auto; scrollbar-base-color: #ffeaff; } table { table-layout: fixed; border-collapse: collapse; background-color: WhiteSmoke; } div#tbl-container table th { width: 120px; } thead th, thead th.locked { font-size: 14px; font-weight: bold; text-align: center; background-color: navy; color: white; border-right: 1px solid silver; position: relative; cursor: default; } thead th {