在asp.net中實現Gridview 表頭固定

來源:互聯網
上載者:User

最近在做項目過程要實現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=/"#/" onclick=/"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=/"#/" onclick=/"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>

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.