asp.net实现固定GridView标题栏的方法(冻结列功能)

2019-05-22 15:18:18于海丽

本文实例讲述了asp.net实现固定GridView标题栏的方法。,具体如下:

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  protected void Page_Load(object sender, EventArgs e)
  {
    DataTable t = new DataTable();
    t.Columns.Add("序号", typeof(int));
    t.Columns.Add("材料", typeof(string));
    t.Columns.Add("单价", typeof(decimal));
    for (int i = 1; i <= 10; i++)
      t.Columns.Add("库存" + i, typeof(int));
    Random rnd = new Random();
    for (int i = 0; i < 80; i++)
    {
      DataRow row = t.NewRow();
      row["序号"] = i + 1;
      row["材料"] = Guid.NewGuid().ToString().Substring(0, 13).ToUpper();
      row["单价"] = rnd.NextDouble() * 100;
      for (int j = 1; j <= 10; j++)
        row["库存" + j] = rnd.Next(10000);
      t.Rows.Add(row);
    }
    GridView1.AutoGenerateColumns = false;
    foreach (DataColumn c in t.Columns)
    {
      BoundField bf = new BoundField();
      bf.DataField = c.ColumnName;
      bf.HeaderText = c.ColumnName;
      if (c.DataType == typeof(decimal))
        bf.DataFormatString = "{0:#,0.00}";
      else if (c.DataType == typeof(int))
        bf.DataFormatString = "{0:#,0}";
      bf.ItemStyle.HorizontalAlign =
        (!string.IsNullOrEmpty(bf.DataFormatString)) ?
        HorizontalAlign.Right : HorizontalAlign.Center;
      GridView1.Columns.Add(bf);
    }
    GridView1.DataSource = t;
    GridView1.DataBind();
  }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title></title>
  <style type="text/css">
  .altRow { background-color: #ddddff; }
  </style>
  <link href="superTables.css" rel="stylesheet" type="text/css" />
  <script type="text/javascript" src="jquery-1.3.1.js"></script>
  <script type="text/javascript" src="superTables.js"></script>
  <script type="text/javascript" src="jquery.superTable.js"></script>
  <script type="text/javascript">
    $(function() {
      $("#GridView1").toSuperTable({ width: "640px", height: "480px", fixedCols: 2 })
      .find("tr:even").addClass("altRow");
    });
  </script>
</head>
<body>
  <form id="form1" runat="server">
  <asp:GridView ID="GridView1" runat="server" Font-Size="9pt" EnableViewState="false">
  </asp:GridView>
  </form>
</body>
</html>

// Super Tables Plugin for jQuery - MIT Style License
// Copyright (c) 2009 Jeffrey Lee --- blog.darkthread.net
//
// A wrapper for Matt Murphy's Super Tables http://www.matts411.com/post/super_tables/
//
// Contributors:
//
////// TO CALL:
// $("...").toSuperTable(options)
//
////// OPTIONS: (order does not matter )
// cssSkin : string ( eg. "sDefault", "sSky", "sOrange", "sDark" )
// headerRows : integer ( default is 1 )
// fixedCols : integer ( default is 0 )
// colWidths : integer array ( use -1 for auto sizing )
// onStart : function ( any this.variableNameHere variables you create here can be used later ( eg. onFinish function ) )
// onFinish : function ( all this.variableNameHere variables created in this script can be used in this function )
// margin, padding, width, height, overflow...: Styles for "fakeContainer"
//
////// Example:
// $("#GridView1").toSuperTable(
//       { width: "640px", height: "480px", fixedCols: 2,
//        onFinish: function() { alert('Done!'); } })
// jquery.superTable.js
(function($) {
  $.fn.extend(
      {
        toSuperTable: function(options) {
          var setting = $.extend(
          {
            width: "640px", height: "320px",
            margin: "10px", padding: "0px",
            overflow: "hidden", colWidths: undefined,
            fixedCols: 0, headerRows: 1,
            onStart: function() { },
            onFinish: function() { },
            cssSkin: "sSky"
          }, options);
          return this.each(function() {
            var q = $(this);
            var id = q.attr("id");
            q.removeAttr("style").wrap("<div id='" + id + "_box'></div>");
            var nonCssProps = ["fixedCols", "headerRows", "onStart", "onFinish", "cssSkin", "colWidths"];
            var container = $("#" + id + "_box");
            for (var p in setting) {
              if ($.inArray(p, nonCssProps) == -1) {
                container.css(p, setting[p]);
                delete setting[p];
              }
            }
            var mySt = new superTable(id, setting);
          });
        }
      });
})(jQuery);