最近在做项目过程要实现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>