GridView固定表头的方法两则

方法一:

<head runat="server">
<title>无标题页</title>
<style>
 .Freezing
  {
  position:relative ;
  table-layout:fixed;
  top:expression(this.offsetParent.scrollTop);  
  z-index: 10;
 }
 .Freezing th{text-overflow:ellipsis;overflow:hidden;white-space: nowrap;padding:2px;}
</style>

</head>
 
 <asp:GridView ID="GridView1" runat="server" CellPadding="4" DataSourceID="SqlDataSource1" 
ForeColor="#333333" GridLines="None">
......
  <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" CssClass="Freezing" />
......
</asp:GridView>

实现方法极其简单,缺点是没有水平滚动功能,如果表格很大的话就不方便了。

方法二:

protected void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack)
  {
     GridView1.Attributes.Add("style", "table-layout:fixed"); //表格必须是固定大小
  }
}



<head runat="server">
<title>无标题页</title>
<script type="text/javascript">
function init()
{
  var bodyGridView = document.getElementById("<%=GridView1.ClientID%>");
  var headGridView= bodyGridView.cloneNode(true);
   
  for(i = headGridView .rows.length -1;i > 0;i--)
  headGridView .deleteRow(i); //删掉数据行
   
  bodyGridView.deleteRow(0); //删掉表头行
  headdiv.appendChild(headGridView); 
}
   
window.onload = init
</script>

</head>
 
 <div id="headdiv" style="top: 16px; left: 152px; position: absolute; width: 328px;
  height: 32px; overflow-x: hidden; overflow-y: scroll">


  //不需要显示表头水平滚动条
 </div>
 <div id="bodydiv" style="top: 45px; left: 152px; position: absolute; width: 328px;
  height: 200px; overflow: auto" οnscrοll="headdiv.scrollLeft=scrollLeft">

 
  //表体的水平滚动条拖动触发表头的水平滚动事件
 <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" CellPadding="4"
  AutoGenerateColumns="False" Font-Names="airl" Font-Size="12px"
  ForeColor="#333333" AllowSorting="True">
......  
 <Columns>
  <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"
  SortExpression="ID">
  <HeaderStyle Width="20px" /> //必须定义表头和表体相同的宽度
  <ItemStyle Width="20px" />
  </asp:BoundField>
  <asp:BoundField DataField="GongYingShangBianHao" HeaderText="GongYingShangBianHao"
  SortExpression="GongYingShangBianHao">
  <HeaderStyle Width="300px" />
  <ItemStyle Width="300px" />
  </asp:BoundField>


......


</Columns>


......


</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:GIFTConnectionString %>"
  SelectCommand="SELECT......">
</asp:SqlDataSource>
</div>


原文链接
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值