最近在做項目過程要實現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>