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

Tagged as

Grid View with Fixed Header

, 11 Mar 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
This article provides you with the solution of scrolling Grid View with fixed Header in ASP.NET.


GridView control of ASP.NET makes our task much simpler as compared to HTML tables. But one problem which I always faced with GridView control is of scrolling headers. I tried several forums and websites, but didn’t come up with a good solution.

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 horizontally.
  • GridView can be scrolled vertically.



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.

Wrapping the GridView in a Div (say DataDiv) and making the DataDiv overflow property to auto solves the problem of scrolling in GridView, but one problem is that the header of the GridView also scrolls up while scrolling it vertically (this is because, the DataDiv is actually scrolling but not the GridView).

To get the solution of this problem, put another Div (say HeaderDiv) above DataDiv.

The next step is to create a new HTML table in HeaderDiv which will contain the new header for the GridView. This job will be done by a JavaScript function (say CreateGridHeader()). This JavaScript function also sets the HeaderDiv style as per the DataDiv and new HTML table style as per GridView style and also we will hide the original header of GridView. This function will be called every time after filling the GridView with data as well as on the Page Load.

At this point, we will have header (new HTML table) and GridView as different entities.

Since, header and GridView both are different entities; therefore, one thing which is still left is how to scroll the header while scrolling the GridView horizontally. This job will be done by a JavaScript function (say Onscrollfnction ()) which will be executed at the scroll event of the DataDiv. This function will scroll the HeaderDiv along with the DataDiv while scrolling in the horizontal direction.


The main objective behind the overall exercise is to create the new table (which contains the new header row) above the GridView with the similar style attributes of the original header and hides the original header of GridView.

Using the Code

  1. The first thing we have to do is to put the GridView inside the Div tag and also define a Header Div which will contain the new Header as follows:
      <%--Div contains the new header of the GridView--%>
      <div id="HeaderDiv">
      <%--Wrapper Div which will scroll the GridView--%>
      <div id="DataDiv" style="overflow: auto; border: 1px solid olive; 
    	width: 600px; height: 300px;" onscroll="Onscrollfnction();">
           <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" 
                  CellPadding="6" CssClass="GridViewStyle">
                  <HeaderStyle  CssClass="GridViewHeaderStyle" />

    Please note the following points in the above code:

    1. GridView1 is the GridView which contains the data.
    2. DataDiv will make the GridView scrollable (horizontally as well as vertically). To make it scrollable, we have to set its overflow property to auto.
    3. I put a JavaScript function say Onscrollfnction () on DataDiv scrolling. This function will be executed every time when the DataDiv scrolls. The main idea behind this function is to scroll the HeaderDiv along with the DataDiv.
    4. HeaderDiv will contain the new header for GridView.
    5. Except this, other styling is not necessary. I just use all other styling for display purposes.
  2. In the second step, write a JavaScript function Onscrollfnction(), which will be executed when you scroll the DataDiv horizontally.
        function Onscrollfnction() 
            var div = document.getElementById('DataDiv'); 
            var div2= document.getElementById('HeaderDiv'); 
            //****** Scrolling HeaderDiv along with DataDiv ******
            div2.scrollLeft = div.scrollLeft; 
            return false;

    This function will scroll the HeaderDiv along with the DataDiv while scrolling in horizontal direction.

  3. The third step is to write a Javascript function CreateGridHeader(). This function will create a new header table for the GridView in HeaderDiv and hides the original header. This function also resolves the style issues between HeaderDiv and DataDiv. This function will be called every time after filling the GridView with data as well as on the Page Load.
        function CreateGridHeader(DataDiv, GridView1, HeaderDiv) 
            var DataDivObj = document.getElementById(DataDiv);
            var DataGridObj = document.getElementById(GridView1);
            var HeaderDivObj = document.getElementById(HeaderDiv);
            //********* Creating new table which contains the header row ***********
            var HeadertableObj = HeaderDivObj.appendChild(document.createElement('table'));
   = '0px';
            var DataDivWidth = DataDivObj.clientWidth;
   = '5000px';
            //********** Setting the style of Header Div as per the Data Div ************
            HeaderDivObj.className = DataDivObj.className;
            //**** Making the Header Div scrollable. *****
   = 'auto'; 
            //*** Hiding the horizontal scroll bar of Header Div ****
            //*** this is because we have to scroll the Div along with the DataDiv.  
   = 'hidden';
            //**** Hiding the vertical scroll bar of Header Div **** 
   = 'hidden'; 
   = DataGridObj.rows[0].clientHeight + 'px';
            //**** Removing any border between Header Div and Data Div ****
   = '0px'; 
            //********** Setting the style of Header Table as per the GridView ************
            HeadertableObj.className = DataGridObj.className;
            //**** Setting the Headertable css text as per the GridView css text 
            HeadertableObj.border = '1px';
            HeadertableObj.rules = 'all';
            HeadertableObj.cellPadding = DataGridObj.cellPadding;
            HeadertableObj.cellSpacing = DataGridObj.cellSpacing;
            //********** Creating the new header row **********
            var Row = HeadertableObj.insertRow(0); 
            Row.className = DataGridObj.rows[0].className;
   = DataGridObj.rows[0].style.cssText;
   = 'bold';
            //******** This loop will create each header cell *********
            for(var iCntr =0; iCntr < DataGridObj.rows[0].cells.length; iCntr++)
                var spanTag = Row.appendChild(document.createElement('td'));
                spanTag.innerHTML = DataGridObj.rows[0].cells[iCntr].innerHTML;
                var width = 0;
                //****** Setting the width of Header Cell **********
                if(spanTag.clientWidth > DataGridObj.rows[1].cells[iCntr].clientWidth)
                    width = spanTag.clientWidth;
                    width = DataGridObj.rows[1].cells[iCntr].clientWidth;
           = width + 'px';
           = width + 20 + 'px';
                DataGridObj.rows[1].cells[iCntr].style.width = width + 'px';
            var tableWidth = DataGridObj.clientWidth;
            //********* Hidding the original header of GridView *******
            DataGridObj.rows[0].style.display = 'none';
            //********* Setting the same width of all the components **********
   = DataDivWidth + 'px';
   = DataDivWidth + 'px';    
   = tableWidth + 'px';
   = tableWidth + 20 + 'px';
            return false;

    This function will take three parameters DataDiv, GridView, and HeaderDiv. This function needs to be called from the server side every time the Grid is filled with data and also on the Page Load.

  4. The final step is to fill the GridView with Data and call the JavaScript function CreateGridHeader () from server side. This JavaScript function is also called on page load.
    string conString = "Provider=Microsoft.JET.OLEDB.4.0; 
    data source=" + Server.MapPath (string.Empty)  + @"\Database\Northwind.mdb";
    string sqlString = "SELECT * FROM CUSTOMERS";
    OleDbConnection conn = new OleDbConnection(conString);
    DataSet ds = new DataSet();
    OleDbDataAdapter adapter = new OleDbDataAdapter(sqlString, conn);
    GridView1.DataSource = ds.Tables[0];
    ClientScript.RegisterStartupScript(this.GetType(), "CreateGridHeader", 
    "<script>CreateGridHeader('DataDiv', 'GridView1', 'HeaderDiv');</script>");
  5. The CreateGridHeader () function needs to be called on Page Load also.
     protected void Page_Load(object sender, EventArgs e)
           if (GridView1.Rows.Count > 0)
                   	"<script>CreateGridHeader('DataDiv', 'GridView1', 

You are now ready with the GridView with fixed headers.

Points of Interest

This solution is tested well with Internet Explorer, Mozilla Firefox, and Google Chrome.


  • Updated on 5-March-2010


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


About the Author

Prakash Varun 2010
Architect Inca Informatics
India India
No Biography provided

Comments and Discussions

QuestionERROR Pinmemberdharshuu3-Nov-14 23:44 
QuestionGridView With Fixed Headers in Asp.Net Using C# and jQuery PinmemberBinu198512-Jun-14 21:38 
QuestionReg : Window Resize [modified] PinmemberKarthik Devaraj24-May-14 0:21 
QuestionCreateGridHeader not working on IE 9 Pinmembergarima sidher24-Feb-14 18:17 
Generalworks great Pinmemberkeremkocal26-Dec-13 5:45 
Questionfixed header grid view with ability of select button PinmemberMember 100642883-Nov-13 21:35 
GeneralReg. Thanks Pinmemberrajabe10-Oct-13 20:51 
GeneralMy vote of 5 Pinmemberhsakarp4-Sep-13 4:36 
QuestionNot Working PinmemberHitesh Vaghasiya15-Aug-13 21:11 
GeneralMy vote of 3 PinmemberMember 236277429-Jul-13 22:45 
GeneralMy vote of 5 PinmemberP. Gopinathan9-Jul-13 21:20 
Questionproblem: selected index changed event is firing only once PinmemberGBVSivaRamKumar20-Mar-13 1:16 
QuestionHeader not visible on page load Pinmemberixashish4-Feb-13 20:31 
QuestionDemo file is correct ? PinmemberMatin11021-Jan-13 7:52 
QuestionFreeze 1st 4 columns of Gridview along with Frozen headers Pinmembersarada ganesh9-Nov-12 6:12 
QuestionIE9 fix Pinmembergringoman130-Oct-12 3:59 
AnswerRe: IE9 fix PinmemberTony Dong13-Feb-13 13:29 
GeneralMy vote of 4 PinmemberFlyntMD25-Oct-12 8:39 
GeneralMy vote of 5 Pinmemberwsc091826-Aug-12 23:06 
GeneralMy vote of 5 PinmemberNitin Jain 100521-Jun-12 8:22 
QuestionUsing in Masterpage PinmemberBpwill18-Jun-12 8:51 
GeneralMy vote of 5 Pinmembertejashri.gandhi10-May-12 3:32 
Thanks..its works great for DataGrid as well..
GeneralMy vote of 5 PinmemberNilesh Patil Kolhapur16-Apr-12 21:27 
QuestionHow to get onscroll event for div ? Pinmemberpmahanta10-Apr-12 21:55 
SuggestionExcellent but when come to IE 9.0 it's changing design PinmemberBojjaiah28-Mar-12 0:53 
GeneralThank you PinmemberDarshanKumar26-Mar-12 0:49 
QuestionNice but column width and header width not equal PinmemberabtinC# 20093-Feb-12 22:47 
GeneralMy vote of 1 PinmemberPratik_Vish30-Jan-12 1:17 
GeneralRe: My vote of 1 PinmemberPrakash Varun 201017-Apr-12 8:45 
QuestionNot working with Updatepanel PinmemberMember 810652128-Dec-11 19:35 
GeneralMy vote of 5 PinmemberAytha Raki14-Dec-11 20:22 
QuestionAnyone know why the example code stopped working in IE9? Pinmemberjnewquis28-Sep-11 10:30 
Generalsignificance of '5000px' Pinmembernirmalsat5-May-11 8:37 
GeneralPaging Not working PinmemberJayakumar23-Mar-11 6:07 
GeneralError PinmemberJayakumar22-Mar-11 21:36 
GeneralRe: Error PinmemberMember 826116926-Sep-11 21:22 
QuestionNot working for table with too many columns? PinmemberMember 76507804-Feb-11 4:10 
GeneralMy vote of 5 Pinmemberahsan sarfraz24-Jan-11 2:07 
GeneralAwsome! Help me a lot! Thank you! Pinmemberdesheng li8-Dec-10 10:59 
GeneralSorting feature PinmemberMargarita186-Oct-10 13:02 
GeneralRe: Sorting feature PinmemberMargarita186-Oct-10 13:08 
GeneralRe: Sorting feature PinmemberPrakash Varun 20106-Oct-10 19:57 
GeneralMy vote of 4 PinmemberSebastien Termote5-Aug-10 3:14 
GeneralPager and Footer Style Pinmemberonur_odemis22-Jun-10 7:08