SIGN UP MEMBER LOGIN:    
ARTICLE

GridView with Fixed Header in ASP.NET

Posted by Dorababu M Articles | ASP.NET Controls in C# September 06, 2011
In this article, I am trying to solve the problem of scrolling headers in ASP.NET GridView control.
Reader Level:
Download Files:
 

Introduction

I have seen many articles on the GridView control with scrolling headers. I tried several forums and websites, but didn't come up with a good solution that work well enough. Some work with the browser compatibility and some others don't work

In this article, I am trying to solve the problem of scrolling headers in the  ASP.NET GridView control.

This article will fulfill the following requirements:

  • GridView will have fixed header.

  • GridView can be scrolled vertically.

Initial view of GridView when loaded

GridView in ASP.NET
Final view of GridView when scrolls down

ASP.NET GridView
Overview

GridView doesn't have the ability to scroll. But if the GridView contains a larger number of rows or columns (say more than 100 rows and 15 columns), we want it to have scrollbars.

Since the Div control has the ability to scroll horizontally and vertically, therefore, to achieve scrolling in GridView, we have to wrap the GridView in the Div control. It is the Div that actually scrolls, but it looks like the GridView is scrolling.

Using the Code

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta http-equiv='X-UA-Compatible' content='IE=7' />
    <link rel='stylesheet' type='text/css' href='Styles/StaticHeader.css' />
    <title></title>
 
    <script type='text/javascript' src='Styles/x.js'></script>
 
    <script type='text/javascript' src='Styles/xtableheaderfixed.js'></script>
 
    <script type='text/javascript'>
        xAddEventListener(window, 'load',
            function() { new xTableHeaderFixed('gvTheGrid', 'table-container', 0); }, false);
    </script>
 
    <
div id='table-container'>
        <asp:GridView ID="gvTheGrid" runat="server" GridLines="Both" CellPadding="3" AutoGenerateColumns="false"
            BackColor="WhiteSmoke" AlternatingRowStyle-BackColor="Silver" HeaderStyle-Font-Size="Medium"
            OnPreRender="gvTheGrid_PreRender" CssClass="gvTheGrid">
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="ID" HeaderStyle-Width="60" ItemStyle-Width="60" />
                <asp:BoundField DataField="Name" HeaderText="Name" HeaderStyle-Width="60" ItemStyle-Width="60" />
                <asp:BoundField DataField="Price" HeaderText="Price" HeaderStyle-Width="60" ItemStyle-Width="60" />
                <asp:BoundField DataField="Description" HeaderText="Description" HeaderStyle-Width="200"
                    ItemStyle-Width="200" />
            </Columns>
        </asp:GridView>
    </div>

Reference

The main reference I have taken is this from site to fix the issue.

http://cross-browser.com/x/examples/xthf-demo.php?s=sep&n=3

Login to add your contents and source code to this article
share this article :
post comment
 

Thank you very much. It is a great article!! Works!

Posted by Natalia Tarasova Mar 10, 2012

Anyone know how to freeze the first column of the grid? In my case I have to scroll right and down.

Posted by Olatz Alberdi Mar 07, 2012

I have the same problem with master page. Send me this code please, if your problem was solved.

Posted by Natalia Tarasova Nov 19, 2011

Check now i attached the master page code too which is working well for me

Posted by Dorababu M Sep 07, 2011

in master page, but I also tried to include it inside the <script> tag (in the current gridview's page).

Posted by Jean Paul Olvera Sep 07, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor