Click here to Skip to main content
15,881,803 members
Articles / Web Development / XHTML

Gridview Fixed Headers

Rate me:
Please Sign up or sign in to vote.
4.00/5 (6 votes)
7 May 2009CPOL3 min read 111.2K   12   17
Excel like Fixed headers for Gridview - Works for many columns which current solutions don't

Introduction

Gridview is one of the good controls which enables us to produce Excel like output in the webforms. We had a scenario where we were supposed to give freeze pane like feature for Gridview. We had a lengthy Grid with some 20 or 30 columns. We had paging for about 20 records per page. So following were the requirements:

  1. Grid must have a Fixed header. 
  2. It should be scrollable horizontally. 
  3. It should be scrollable vertically.

Background

You can learn more about gridview in the following links:

Using the Code

I did a lot of searching and could only get solutions that worked where the columns are limited. So I did a small R&D and derived at a solution which I will be sharing now. Take for example a Timesheet application and it has a Grid. We are going to give a fixed header feature for this Grid. The GridView will look as follows:

ASP.NET
<asp:GridView ID="grdTask" runat="server" GridLines  ="Both"    Width ="100%"  > 
            <!-- Some column Definitions--> 
</asp:GridView> 

The first thing we will be doing is to move this Grid inside a Div tag.

ASP.NET
<div  id = "AdjResultsDiv"> 
<asp:GridView ID="grdTask" runat="server" GridLines  ="Both"    Width ="100%"  > 
            <!-- Some column Definitions--> 
</asp:GridView>
</Div>

Let's say we have a StyleSheet file "Style.Css". Please add the following styles for the Div using ID based styles. You can also use Class based styles.

CSS
div#MyGrid { 
width: 1080px; 
height: 500px; 
overflow: scroll; 
position: relative; 
}

The above code will make sure of the following things:

  1. We can define the height and the width of the Div we are using. 
  2. The scrollbars that we may need or not using the Overflow property. 
  3. Setting the Position to relative makes the Div relative to its parent.

The next thing will be the styles for the Grid. By default the grid will be rendered as a table with default Div tags as:

ASP.NET
<div> 
<table ......   

So when you add a Div Tag before the Grid, it will be rendered as:

ASP.NET
<div id = "AdjResultsDiv">

<div> 
<table ...... 

So our styles should be accordingly designed. I have given the Grid styles below:

CSS
div#MyGrid th 
{   
top: expression(document.getElementById("AdjResultsDiv").scrollTop-2); 
left: expression(parentNode.parentNode.parentNode.parentNode.scrollLeft); 
position: relative; 
z-index: 20; 
} 

Following are the functionalities:

  1. Give the Top and Left coordinates of the TH tag which will be generated for the Header inside the Grid. We are using the expression tag to dynamically assign the values through JavaScript.
  2. Place the Grid relatively inside the Div tag. If this property is not set, the Header will go out of the Div Box.
  3. Set the Z-Index of the Grid. The z-index property sets the stack order of an element. So based on the order, they will be placed in front.

So add the following in the Stylesheet:

CSS
div#AdjResultsDiv { 
width: 1080px; 
height: 500px; 
overflow: scroll; 
position: relative; 
}

div#AdjResultsDiv th {   
top: expression(document.getElementById("AdjResultsDiv").scrollTop-2); 
left: expression(parentNode.parentNode.parentNode.parentNode.scrollLeft); 
position: relative; 
z-index: 20; 
} 

And put the Grid inside the Div tag. That's all. You are now ready with the Fixed headers for the Grid.

Points of Interest

I noticed a very interesting thing when making this feature. Initially I got a large set of styles and tried to use them. But the Gridview did not show the fixed headers. So I did a small analysis about the position Relative concept and used it for the Div tag and the Grid's Table intelligently.

History

  • 7th May, 2009: Initial post

I will keep this post updated for the Fixed columns for the GridView.

License

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


Written By
Software Developer (Senior) Cognizant technology Solutions
India India
I love open source development. I used to go through technical updates from dotnet, try new softwares from community and evaluate them. I like to develop new programs that helps in my daily routine works and would blog them for my future reference. I used to reply fro questions in forums like codeprject,satckoverflow and asp.net.

I have my personal blog at http://www.codetherasa.in/

Comments and Discussions

 
QuestionIt didn't work Pin
Member 1014643225-Jun-15 2:16
Member 1014643225-Jun-15 2:16 
QuestionGood Job Pin
aspnetgemini19-Jun-14 13:01
aspnetgemini19-Jun-14 13:01 
QuestionShow Demo Pin
Member 992560818-Aug-13 8:24
Member 992560818-Aug-13 8:24 
Questionnot working in ie9 with master & content page Pin
Dattatray.Pise26-Jun-13 0:15
Dattatray.Pise26-Jun-13 0:15 
BugDoes working in All IE browsers? Pin
poomani4u27-Feb-13 1:56
poomani4u27-Feb-13 1:56 
QuestionFacing Problem in Gridview Fixed Header [modified]? Pin
Gyana Ojha26-Apr-11 20:44
Gyana Ojha26-Apr-11 20:44 
GeneralMy vote of 4 Pin
avinpanwar307-Apr-11 23:07
avinpanwar307-Apr-11 23:07 
i think not only its simple but also work so no nice
GeneralMy vote of 5 Pin
plcasey7-Aug-10 21:43
plcasey7-Aug-10 21:43 
GeneralThe header not scrolling horizontally Pin
vmanick26-May-10 4:38
vmanick26-May-10 4:38 
Generalfreezing column Pin
captain121017-Feb-10 9:36
captain121017-Feb-10 9:36 
Questiondoes it work on browsers other than IE? Pin
mostafa_mostafa10-May-09 5:55
mostafa_mostafa10-May-09 5:55 
AnswerRe: does it work on browsers other than IE? Pin
Thanigainathan.S10-May-09 8:12
Thanigainathan.S10-May-09 8:12 
GeneralRe: does it work on browsers other than IE? Pin
poomani4u27-Feb-13 0:52
poomani4u27-Feb-13 0:52 
GeneralRe: does it work on browsers other than IE? Pin
Thanigainathan.S7-May-13 0:11
Thanigainathan.S7-May-13 0:11 
GeneralGreat work! Pin
ouyanglibing7-May-09 20:03
ouyanglibing7-May-09 20:03 
GeneralRe: Great work! Pin
Thanigainathan.S7-May-09 21:16
Thanigainathan.S7-May-09 21:16 
GeneralRe: Great work! Pin
jportelas12-May-09 12:20
jportelas12-May-09 12:20 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.