在asp.net中实现Gridview 表头固定

最近在做项目过程要实现Gridview 表头固定 ,采用了以下方法。

<script language="javascript"> 
 function init()
          {
           var tbBody = document.getElementById("<%=GridView2.ClientID%>");
            if(tbBody!=null)
           {
           var tbHead = tbBody.cloneNode(true)
           for(i = tbHead.rows.length -1;i > 1;i--)
            tbHead.deleteRow(i);
         
           divContent.appendChild(tbHead);
           }
          }
          window.onload = init
  </script>
  <div id="divContent">
   
          </div>
          <div id ="divG" style="height: 180px;position: relative; overflow:scroll;">
 <asp:GridView ID="GridView1" runat="server" OnRowCreated="GridView1_OnRowCreated" AllowPaging="true" PageSize="15"
      AutoGenerateColumns="false"  Width="800" ShowHeader ="false"  OnRowDataBound="GridView1_RowDataBound"  DataKeyNames="XTBH">
     <AlternatingRowStyle CssClass="Alternatingback"></AlternatingRowStyle>
        <RowStyle CssClass="griditem"></RowStyle>
         <HeaderStyle BackColor="#CEDDF0" ForeColor="#000000"  Height="26px" />
        <Columns>
            <asp:BoundField HeaderText="企业名称" DataField="Organization" />
             <asp:TemplateField>
            <ItemTemplate>
             <%# DataBinder.Eval(Container.DataItem, "XTBH").ToString().Trim() == "0" ? "" + DataBinder.Eval(Container.DataItem, "Mingc") + "" : "<a href=/"#/" οnclick=/"window.open('../project/#.aspx?XTBH=" + DataBinder.Eval(Container.DataItem, "XTBH") + "','','toolbar=no,scrollbars=yes')/">" + DataBinder.Eval(Container.DataItem, "Mingc") + "</a>"
             %>
            </ItemTemplate>
            </asp:TemplateField>
         
           <asp:BoundField DataField="ZJF" HeaderText="总额(万)"/>  
                <asp:BoundField DataField="Daik" HeaderText="贷款(万)"/>
                   <asp:BoundField DataField="Gdzc" HeaderText="固定资产投资"/>  
                 <asp:BoundField DataField="YongH" HeaderText="其中用汇(万)"/>    
                
        </Columns>
         <PagerSettings Visible ="false" />
        <EmptyDataTemplate>
         <tr class="gridheader" style="font-size:10pt;">
         <th rowspan="2">企业名称</th><th rowspan="2">项目名称</th><th colspan="4">总投资</th></tr><tr  class="gridheader" style="font-size:10pt;"></th><th>总额(万)</th><th>其中贷款(万)</th><th>其中固定资产投资(万)</th><th>其中用汇(万)</th>
  </tr>
        </EmptyDataTemplate>
        <EmptyDataRowStyle CssClass="gridheader" />
   
    </asp:GridView>
</div>

  <div id="div2" style="visibility: hidden;height:180;" >
      <asp:GridView ID="GridView2"  runat="server"   OnRowCreated="GridView2_OnRowCreated" AllowPaging="true" PageSize="1"
      AutoGenerateColumns="false"  Width="800" OnRowDataBound="GridView2_RowDataBound"  DataKeyNames="XTBH">
     <AlternatingRowStyle CssClass="Alternatingback"></AlternatingRowStyle>
        <RowStyle CssClass="griditem"></RowStyle>
         <HeaderStyle BackColor="#CEDDF0" ForeColor="#000000"  Height="26px" />
        <Columns>
 <asp:BoundField HeaderText="企业名称" DataField="Organization" />
             <asp:TemplateField>
            <ItemTemplate>
             <%# DataBinder.Eval(Container.DataItem, "XTBH").ToString().Trim() == "0" ? "" + DataBinder.Eval(Container.DataItem, "Mingc") + "" : "<a href=/"#/" οnclick=/"window.open('../project/#.aspx?XTBH=" + DataBinder.Eval(Container.DataItem, "XTBH") + "','','toolbar=no,scrollbars=yes')/">" + DataBinder.Eval(Container.DataItem, "Mingc") + "</a>"
             %>
            </ItemTemplate>
            </asp:TemplateField>
         
           <asp:BoundField DataField="ZJF" HeaderText="总额(万)"/>  
                <asp:BoundField DataField="Daik" HeaderText="贷款(万)"/>
                   <asp:BoundField DataField="Gdzc" HeaderText="投资"/>  
                 <asp:BoundField DataField="YongH" HeaderText="用汇"/>    

  </Columns>
         <PagerSettings Visible ="false" />
        </asp:GridView>
        </div>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值