Click here to Skip to main content
Licence 
First Posted 8 Apr 2005
Views 278,765
Bookmarked 89 times

Fixed header in ASP.NET DataGrid

By KjellSJ | 20 Apr 2005
How to get a fixed header row in an ASP.NET DataGrid?
3 votes, 7.0%
1
1 vote, 2.3%
2

3
9 votes, 20.9%
4
30 votes, 69.8%
5
4.69/5 - 43 votes
3 removed
μ 4.33, σa 1.72 [?]

Introduction

A very common design request is to create a DataGrid list, that can contain many rows (e.g. account contacts list), that has a fixed maximum height and a scrollbar when there are more rows than can fit into the allotted space. This is easily done by putting the DataGrid within a DIV tag:

<DIV style="OVERFLOW: auto; HEIGHT: 
120px">
<asp:DataGrid ...
</DIV>

Then the next design request made by customers is to have a scrolling body with fixed column headers so that, users don't have to remember which column contains what. Such a fixed header that stays put and never scroll out of view can be made using a style like this:

<style type="text/css">
<!--
.DataGridFixedHeader {background-color: white; position:relative; top:expression(this.offsetParent.scrollTop);}
-->
</style>

The background color is needed to hide the data rows as they scroll under the header row. Make sure that the style is on a single line, the expression might not work when the style definition uses multiple lines.

Apply the new header style to the DataGrid using the HeaderStyle element:

<asp:DataGrid id="dgContacts" runat="server" ... >
...
<HeaderStyle CssClass="ms-formlabel DataGridFixedHeader"></HeaderStyle>
...

Note how can you apply multiple classes within the CssClass attribute to combine several styles to achieve reuse in your web pages or to combine them with styles that are not your own (such as SharePoint styles).

There are several other suggestions out there on how to achieve a fixed header row, such as having a separate table above the DataGrid, but this suffers from alignment and column width problems, for e.g., when a table cell contains non-breaking content. It also creates more work when changing the columns of the DataGrid. The style solution in this article, however, is as simple as it gets.

Supported Browsers

This solution is based on CSS expressions, and works with MSIE 5.x and 6.x. It is pure client-side and should not interfere with server-side code (except for adding the style), but it might behave strange if you have other positioning stuff in your page.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

KjellSJ

Web Developer

Norway Norway

Member
My blog: kjellsj.blogspot.com
My contribution to other stuff at CodeProject:
PleaseWaitButton

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. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionCan I also alter the color of Visited Link in the datagrid header? Pinmembersmcirish9:09 22 Apr '08  
AnswerRe: Can I also alter the color of Visited Link in the datagrid header? Pinmembernsimeonov15:55 5 Dec '08  
GeneralFixed Grid Header with Scrollable Data Pinmemberaswini2:22 9 Apr '08  
Generaldont see border in head datagrid PinmemberRoxanaAFG10:09 10 Oct '07  
GeneralRe: dont see border in head datagrid PinmemberMarkShoe6:35 12 Oct '07  
GeneralRe: dont see border in head datagrid Pinmemberjigscool_2211:44 25 Feb '08  
GeneralRe: dont see border in head datagrid Pinmemberhasitha1230:04 20 Mar '08  
GeneralRe: dont see border in head datagrid PinmemberTheSoundofCylons4:23 3 Jun '08  
GeneralRe: dont see border in head datagrid Pinmemberrterry8:27 3 Nov '09  
GeneralRe: dont see border in head datagrid PinmemberTheSoundofCylons9:11 3 Nov '09  
GeneralDIV + Fixed Header Not working Pinmembersbadriprasad22:13 21 Sep '07  
QuestionHow to get Vertical Scroll bar only Pinmembersmcirish8:59 21 Aug '07  
AnswerRe: How to get Vertical Scroll bar only PinmemberMember 446923410:32 13 Jan '11  
GeneralFixed Header and Fixed Pager(or Footer) as well Pinmemberahmedelbatal4:43 22 Jul '07  
Generalexpression not working on Mozila Pinmembersmruti_ranjan0:52 19 Apr '07  
GeneralEditing items that you must scroll to see Pinmemberevshell186:37 13 Apr '07  
GeneralRe: Editing items that you must scroll to see Pinmemberevshell186:02 16 Apr '07  
GeneralRe: Editing items that you must scroll to see Pinmemberqtran02039:23 31 May '07  
Generaldon't use top: expression PinmemberShiggity9:49 30 Mar '07  
GeneralRe: don't use top: expression PinmemberDebo_Net21:41 9 Sep '07  
GeneralWatch Positioning Pinmemberjohnsaxton@yahoo.com4:03 15 Nov '06  
GeneralDatagrid Header Pinmembernour1231:24 10 Nov '06  
AnswerRe: Datagrid Header Pinmembersmcirish9:06 21 Aug '07  
Generalcheckbox in gridview PinmemberRonjon Kumar Ghosh0:01 17 Sep '06  
Questionfixed header gridview PinmemberSultan Khan23:38 16 Sep '06  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120209.1 | Last Updated 21 Apr 2005
Article Copyright 2005 by KjellSJ
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid