Click here to Skip to main content
Click here to Skip to main content

Gridview with Fixed Header

By , 15 Sep 2011
 

Introduction

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

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

This article will fulfill the following requirements:

  • GridView will have fixed header.
  • GridView can be scrolled vertically.

Initially view of GridView when loaded

image001.jpg

Final view of GridView when scrolls down

image001.jpg

Overview

GridView doesn’t have the ability to scroll. But if the GridView contains the 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 to fix the issue is from this site:

History

  • 6th September, 2011: Initial version
  • 15th September, 2011: Code with paging added

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

demouser743
Software Developer
India India
Member
I am Dorababu, working as a Software engineer. Web development in Asp.Net with C#, WinForms and MS sql server are the experience tools that I have had for the past 3 years. Yet to work on WCF, WPF, Silverlight and other latest ones.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionVery tanksmemberMember 816385722 Dec '12 - 20:48 
very useful way for fix header.
i use a paging control component. when change page the header not change. can you help me?
AnswerRe: Very tanksmemberDorababu74327 Dec '12 - 0:13 
Are you using custom paging?
GeneralRe: Very tanksmemberMember 816385729 Dec '12 - 0:28 
yes. a costume paging with Features such as :combo record in page, next page and Previous Page
GeneralMy vote of 4memberMember 84554365 Sep '12 - 2:50 
very helpful for me
BugDoesn't work at all - throws runtime errorsmemberPaulTownsend10 May '12 - 8:36 
It doesn't work at all when I include the js files in my project. It throws a runtime error
 
Error: _ta[i] is null
Source file: http://...xtableheaderfixed.js
Line: 101
 

 
And the example project won't build - the designer files seem to be corrupt. VS throws the error Source File could not be opened (unspecified error)
GeneralGood OnememberMember 48613919 Mar '12 - 5:39 
Thank you for the post. I tested it. It works.
GeneralRe: Good OnememberDorababu74319 Mar '12 - 5:44 
Thank you for your comment Smile | :)
QuestionFixed column alignment is offmemberdesertfoxaz19 Sep '11 - 11:23 
I downloaded your sample and ran it but I'm seeing that the fixed columns don't line up exactly with the grid lines. This does however help demonstrate how it's working. I've tried tweaking the CSS and even some of the grid properties but try as I might, I cannot get the fixed columns and the grid colums to line up so it looks seamless. Is anybody else seeing this behavior? I'm running IE8 on Windows 7.
QuestionNice but after Sorting & Paging, its not workingmemberMember 395260514 Sep '11 - 19:57 
Nice but after Sorting & Paging, its not working.
Gratefull, if u can add functionality.
asdfasdfasdf

AnswerRe: Nice but after Sorting & Paging, its not workingmemberDorababu74315 Sep '11 - 1:31 
Try this in Page render event and let me know
 
<pre>protected void gvTheGrid_PreRender(object sender, EventArgs e)
        {
 
            // You only need the following 2 lines of code if you are not 
            // using an ObjectDataSource of SqlDataSource
            if (!Page.IsPostBack)
            {
                gvTheGrid.DataSource = DataTable();
                gvTheGrid.DataBind();
            }
            if (gvTheGrid.Rows.Count > 0)
            {
                //This replaces <td> with <th> and adds the scope attribute
                gvTheGrid.UseAccessibleHeader = true;
 
                //This will add the <thead> and <tbody> elements
                gvTheGrid.HeaderRow.TableSection = TableRowSection.TableHeader;
 
                //This adds the <tfoot> element. 
                //Remove if you don't have a footer row
                //gvTheGrid.FooterRow.TableSection = TableRowSection.TableFooter;
            }
        }
 
If you are using my example change the css as follows apply height to 100 for
#table-container {
in css
 
Try this code in Page Index Changing
 
 protected void gvTheGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvTheGrid.PageIndex = e.NewPageIndex;
            gvTheGrid.DataSource = DataTable();
            gvTheGrid.DataBind();
        }

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 15 Sep 2011
Article Copyright 2011 by demouser743
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid