jQuery Fixed Header Scrollable GridView

In this example i'm explaining how to create Fixed Header Scrollable Gridview Using jQuery in ASP.NET. add JQuery library and fixed header scrollable gridview plugin in solution and add reference to it between <head></head> section of HTML source of aspx page.

Jquery fixed header scrollable gridview in asp.net
I have used northwind database to populate gridview.

you can also create Scrollable GridView With Fixed Headers Using CSS if you don't want to use jQuery or JavaScript.

<head runat="server">
<title>jQuery Fixed Header Scrollable GridView</title>
<script src="jquery-1.4.1.min.js" type="text/javascript">
<script src="ScrollableGrid.js" type="text/javascript">

Add this JQuery function call between <head></head> section

<script type="text/javascript" language="javascript">
$('#<%=fixedHeaderScrollableGridView.ClientID %>').Scrollable();

Add gridview on aspx page and populate it.

<asp:GridView ID="fixedHeaderScrollableGridView" runat="server" 
<asp:BoundField DataField="ProductID" HeaderText="ProductID"/>
<asp:BoundField DataField="ProductName" HeaderText="ProductName" /> 
<asp:BoundField DataField="QuantityPerUnit" HeaderText="QuantityPerUnit" /> 
<asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" /> 
<asp:BoundField DataField="CategoryName" HeaderText="CategoryName" /> 
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
SelectCommand="SELECT [ProductID], [ProductName], [QuantityPerUnit], 
[UnitPrice], [CategoryName] FROM [Alphabetical list of products]">

Build and Run the application.

If your application uses Master Page then add reference to library and plugin in head ContentPlaceHolder of master page
<head runat="server">
<title>Fixed Header Scrollable GridView With Master Page</title>
<asp:ContentPlaceHolder id="head" runat="server">
<script src="jquery-1.4.1.min.js" type="text/javascript"/>
<script src="ScrollableGrid.js" type="text/javascript"/>
<form id="form1" runat="server">
Add Master Page Content And Design Here 
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">

Call Scrollable() function of jquery plugin in Head ContentPlaceHolderID of content page.
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<script type="text/javascript" language="javascript">
  $('#<%=fixedHeaderScrollableGridView.ClientID %>').Scrollable();
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
Put Gridview Source Here 

Download Sample Code

If you like this post than join us or share


Anonymous said...

Great script. Works fine on a small simple grid, but on a larger grid, the header widths are all out of line with the data. Any idea what might be going on here?


Vivek Kumar Gupta said...

This comment has been removed by a blog administrator.

Unknown said...

Unable to get value of the property 'offsetWidth': object is null or undefined


Unknown said...

@Above: can you post your code ?

Anonymous said...

Good Script works fine on Simple gride, but it's not working on Gride__Sorting(object sender, GridViewSortEventArgs e) in Event . please help me

Anonymous said...

This works great, but when the page posts back (to refresh the grid), the headers are fixed and the div is not scrollable.

Can you help?


Anonymous said...

I tired in my code but its not work

Unknown said...

@Above: What error you are getting ? , did you add jquery and ScrollableGrid javascript files in solution and reference in head section ?

GRoman said...

If you use master page, the head section is only available there, how can I use the function with out calling the function from the head section?

Unknown said...

@GRoman: Add reference to jquery library and fixed header scrollable gridview plugin in Master Page's head ContentPlaceHolder,

And add the function call in head ContentPlaceHolder of your content page

I have updated the article for Master Page implementation as well

Anonymous said...

i tried this code bt it is not working when paging is enabled wt to do plz reply

Unknown said...

@Above : i have working code with paging enabled attached with this post, plz download it and compare with yours

Benjamín said...

Hi, thanks for the example but here you use a simple grid with hardcoded columns, what if the grid's data is loaded from a SQL query? im trying to implement this but as de column's with are different depending on the query's response it is impossible to make them be aligned to the header's columns and the table looks terrible.

sharad omar said...


This is not working in Master Page.
pls check and attached same working code for it.

Sharad Omar

Sudipta..towards a new HOrizon said...

Microsoft JScript runtime error: Object doesn't support this property or method
i am using master page. how to solve

Unknown said...

This worked really well. Thanks for the great post.

Unknown said...

Great work! how to make ScrollHeight as a parameter?

Anonymous said...

hii this code is not working after attaching with mater page

Find More Articles