代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>GridView固定表头 清清月儿http://blog.csdn.net/21aspnet </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>
<body style="font-size=12px">
    <form id="form1" runat="server">
    <div style="overflow-y: scroll; height: 200px;width:300px" id="dvBody">
                   <asp:GridView ID="GridView1" runat="server"    AutoGenerateColumns="False" CellPadding="3" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"
                        OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Size="12px" OnRowCreated="GridView1_RowCreated"  >
                        <FooterStyle BackColor="White" ForeColor="#000066" />
                        <Columns>
                            <asp:BoundField DataField="身份证号码" HeaderText="编号" ReadOnly="True" />
                            <asp:BoundField DataField="邮政编码" HeaderText="邮政编码" SortExpression="邮政编码" />
                            <asp:BoundField DataField="家庭住址" HeaderText="家庭住址"  />
                            <asp:BoundField DataField="姓名" HeaderText="姓名"  />
                            
                        </Columns>
                        <RowStyle ForeColor="#000066" />
                        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left"  CssClass="ms-formlabel DataGridFixedHeader"/>
                        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" CssClass="Freezing"/>
                    </asp:GridView>
        </div>    </form>
</body>
</html> 用法:CSS设如上的样式,HeaderStyle加CssClass="Freezing,套住GridView的Div设置高度宽度 <div style="overflow-y: scroll; height: 200px;width:200px" >

解决方案 »

  1.   

    用次方法的时候它会把我的所有列都展现了,我要的是部分列通过拖动panel下面的滚动条出现的,不是拖动IE窗体下面的滚动条
      

  2.   

    在上下滚动的时候是可以控制到烈头的固定,但是烈头不能是全部展现在IE的窗口上面,我原来的做法是把Grid放在一个Panel里面,固定panel的宽和高,将panel设置为上下左右都能滚动的形式,通过滚动条的左右拖动来展现不能看见的列,但是上下拖动的时候,烈头也会随这滚动,我不知道怎么样将烈头在上下拖动的时候固定主
      

  3.   

    <asp:Panel Width="745" runat="server" ID="panel_temp" ScrollBars="Auto" Width="450px">
                                                    <asp:GridView ID="GridView1" runat="server" AllowSorting="True" OnPageIndexChanging="GridView1_PageIndexChanging"
                                                        OnSorting="GridView1_Sorting" PageSize="5" AutoGenerateColumns="False" Width="2000px"
                                                        BorderColor="SeaShell" BorderStyle="Solid" BorderWidth="1px" CellPadding="2"
                                                        ForeColor="Black" GridLines="Vertical">
                                                        <Columns>
                                                            <asp:BoundField DataField="经销商编码" HeaderText="经销商编码" ReadOnly="True" SortExpression="经销商编码">
                                                                <ItemStyle Width="100px" />
                                                            </asp:BoundField>
                                                            <asp:BoundField DataField="经销商名称" HeaderText="经销商名称" ReadOnly="True" SortExpression="经销商名称">
                                                                <ItemStyle Width="100px" />
                                                            </asp:BoundField>
                                                            <asp:BoundField DataField="经销商类型" HeaderText="经销商类型" ReadOnly="True" SortExpression="经销商类型">
                                                                <ItemStyle Width="100px" />
                                                            </asp:BoundField>
                                                            <asp:BoundField DataField="月份" HeaderText="月份" ReadOnly="True" SortExpression="月份">
                                                                <ItemStyle Width="100px" />
                                                            </asp:BoundField>
                                                            <asp:BoundField DataField="B2C每周拜访指标" HeaderText="B2C每周拜访指标" ReadOnly="True" SortExpression="B2C每周拜访指标">
                                                                <ItemStyle Width="150px" />
                                                            </asp:BoundField>
                                                            <asp:BoundField DataField="SPANCO状态跟踪" HeaderText="SPANCO状态跟踪" ReadOnly="True" SortExpression="SPANCO状态跟踪">
                                                                <ItemStyle Width="150px" />
                                                            </asp:BoundField>
                                                            <asp:BoundField DataField="销售代表应收历史" HeaderText="销售代表应收历史" ReadOnly="True" SortExpression="销售代表应收历史">
                                                                <ItemStyle Width="150px" />
                                                            </asp:BoundField>
                                                            <asp:BoundField DataField="销售代表应收帐龄历史" HeaderText="销售代表应收帐龄历史" ReadOnly="True" SortExpression="销售代表应收帐龄历史">
                                                                <ItemStyle Width="150px" />
                                                            </asp:BoundField>
                                                            <asp:BoundField DataField="经销商每月生意指标" HeaderText="经销商每月生意指标" ReadOnly="True" SortExpression="经销商每月生意指标">
                                                                <ItemStyle Width="150px" />
                                                            </asp:BoundField>
                                                            <asp:BoundField DataField="SPANCO状态汇总" HeaderText="SPANCO状态汇总" ReadOnly="True" SortExpression="SPANCO状态汇总">
                                                                <ItemStyle Width="150px" />
                                                            </asp:BoundField>
                                                            <asp:BoundField DataField="每周KPI指标" HeaderText="每周KPI指标" ReadOnly="True" SortExpression="每周KPI指标">
                                                                <ItemStyle Width="150px" />
                                                            </asp:BoundField>
                                                            <asp:BoundField DataField="销售数据" HeaderText="销售数据" ReadOnly="True" SortExpression="销售数据">
                                                                <ItemStyle Width="150px" />
                                                            </asp:BoundField>
                                                            <asp:BoundField DataField="进销存数据" HeaderText="进销存数据" ReadOnly="True" SortExpression="进销存数据">
                                                                <ItemStyle Width="150px" />
                                                            </asp:BoundField>
                                                            <asp:BoundField DataField="库存数据" HeaderText="库存数据" ReadOnly="True" SortExpression="库存数据">
                                                                <ItemStyle Width="150px" />
                                                            </asp:BoundField>
                                                        </Columns>
                                                        <PagerSettings Visible="False" />
                                                        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                                                        <RowStyle BackColor="#EFF3FB" BorderColor="Black" BorderStyle="Solid" BorderWidth="1px"
                                                            Width="1px" />
                                                        <EditRowStyle BackColor="#2461BF" />
                                                        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                                                        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                                                        <HeaderStyle CssClass="Freezing" BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                                                        <AlternatingRowStyle BackColor="White" />
                                                    </asp:GridView>
                                                </asp:Panel>
      

  4.   

    如果用Repeater,楼主你看这样行不行:外面一个div(用于水平滚动)
    里面上面是列头
    下面是又一个div(用于上下滚动),里面套Repeater显示的数据
      

  5.   

    这样是可以实现,但是用Repeater,就是没有Grid编辑方便