GridView固定表头和首列

 下载:GridView固定表头和首列

关键代码:

        /*固定行头样式*/.fixRowHead
        {
            position: relative;
            left: expression(this.parentElement.parentElement.parentElement.parentElement.parentElement.scrollLeft);
            z-index: 10;
        }
        /*固定表头样式*/.fixColHead
        {
            position: relative;
            top: expression(this.offsetParent.scrollTop);
            z-index: 11;
        }


页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FixHeadAndFirstCol.aspx.cs"
    Inherits="FixHeadAndFirstCol._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>固定表头和首列</title>
    <style type="text/css">
        /*固定行头样式*/.fixRowHead
        {
            position: relative;
            left: expression(this.parentElement.parentElement.parentElement.parentElement.parentElement.scrollLeft);
            z-index: 10;
            
            border-left: solid 1px gray;
            border-top: solid 1px gray;
        }
        /*固定表头样式*/.fixColHead
        {
            position: relative;
            top: expression(this.offsetParent.scrollTop);
            z-index: 11;
            
            background-color: #5D7B9D;
            font-weight: bold;
            color: White;
        }
        .fixColHead th
        {
            position: relative;
            border-left: solid 1px gray;
            border-top: solid 1px gray;
        }
        .gridView
        {
            border-bottom: solid 1px gray;
            border-right: solid 1px gray;
        }
        .gridView td
        {
            border-top: solid 1px gray;
            border-left: solid 1px gray;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div style="font-size: 10pt; margin-bottom: 8px;">
        1、固定表头和首列</div>
    <div style="overflow: auto; width: 500px; height: 300px; position: relative;">
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CssClass="gridView"
            Width="850px" CellPadding="4" GridLines="None">
            <HeaderStyle CssClass="fixColHead" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <Columns>
                <asp:BoundField DataField="x0" HeaderText="列x0">
                    <HeaderStyle CssClass="fixRowHead" />
                    <ItemStyle CssClass="fixRowHead" Width="50px"></ItemStyle>
                </asp:BoundField>
                <asp:BoundField DataField="x1" HeaderText="列x0000001">
                    <ItemStyle Width="100px"></ItemStyle>
                </asp:BoundField>
                <asp:BoundField DataField="x2" HeaderText="列x0000002">
                    <ItemStyle Width="100px"></ItemStyle>
                </asp:BoundField>
                <asp:BoundField DataField="x3" HeaderText="列x0000003">
                    <ItemStyle Width="100px"></ItemStyle>
                </asp:BoundField>
                <asp:BoundField DataField="x4" HeaderText="列x0000004">
                    <ItemStyle Width="100px"></ItemStyle>
                </asp:BoundField>
                <asp:BoundField DataField="x5" HeaderText="列x0000005">
                    <ItemStyle Width="100px"></ItemStyle>
                </asp:BoundField>
                <asp:BoundField DataField="x6" HeaderText="列x0000006">
                    <ItemStyle Width="100px"></ItemStyle>
                </asp:BoundField>
                <asp:BoundField DataField="x7" HeaderText="列x0000007">
                    <ItemStyle Width="200px"></ItemStyle>
                </asp:BoundField>
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>


后台:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace FixHeadAndFirstCol
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BindData();
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        public void BindData()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("x0", typeof(string));
            dt.Columns.Add("x1", typeof(string));
            dt.Columns.Add("x2", typeof(string));
            dt.Columns.Add("x3", typeof(string));
            dt.Columns.Add("x4", typeof(string));
            dt.Columns.Add("x5", typeof(string));
            dt.Columns.Add("x6", typeof(string));
            dt.Columns.Add("x7", typeof(string));


            for (int i = 0; i < 100; i++)
            {
                dt.Rows.Add(i.ToString(), "xx" + i.ToString("0000"), "idt", "xhxxxxh", "xxxx", "dxdxdxxxwwww", "d6", "d77777777777777777777");
            }

            this.GridView1.DataSource = dt;
            this.GridView1.DataBind();
        }
    }
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值