Click here to Skip to main content
15,888,521 members
Articles / Web Development / HTML
Article

Fixed header in ASP.NET DataGrid

Rate me:
Please Sign up or sign in to vote.
4.71/5 (44 votes)
20 Apr 20052 min read 477.5K   92   83
How to get a fixed header row in an ASP.NET DataGrid?

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:

ASP.NET
<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:

ASP.NET
<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.NET
<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


Written By
Web Developer
Norway Norway
My blog: kjellsj.blogspot.com
My contribution to other stuff at CodeProject:
PleaseWaitButton

Comments and Discussions

 
QuestionRe: How to Fix the Footer Pin
Ronjon Kumar Ghosh16-Sep-06 22:50
Ronjon Kumar Ghosh16-Sep-06 22:50 
QuestionTemplateColumn Pin
Kotlet4-Oct-05 4:49
Kotlet4-Oct-05 4:49 
AnswerRe: TemplateColumn Pin
KjellSJ12-Nov-05 4:19
KjellSJ12-Nov-05 4:19 
QuestionMultiline header problem ? Pin
browndog27-Jul-05 9:47
browndog27-Jul-05 9:47 
GeneralFixed Header in DataGird Pin
michelpf21-Jul-05 3:39
michelpf21-Jul-05 3:39 
GeneralRe: Fixed Header in DataGird Pin
KjellSJ31-Jul-05 2:04
KjellSJ31-Jul-05 2:04 
GeneralAnother approach Pin
Anonymous20-Jun-05 17:57
Anonymous20-Jun-05 17:57 
GeneralRows height gets large with few rows Pin
MASTERTECH196420-May-05 6:01
MASTERTECH196420-May-05 6:01 
When my grid gets just one or two rows, the height expands to compensate. Anyway to fix? Also, a slight bit of the row appears above the header, 1 pixel or so, could not stop that. Any ideas?

BTW, great article, great solution, almost for me.

Ed
GeneralRe: Rows height gets large with few rows Pin
KjellSJ20-May-05 6:12
KjellSJ20-May-05 6:12 
GeneralRe: Rows height gets large with few rows Pin
Vaverchak27-May-05 5:01
Vaverchak27-May-05 5:01 
GeneralRe: Rows height gets large with few rows Pin
MASTERTECH196427-May-05 5:22
MASTERTECH196427-May-05 5:22 
GeneralRe: Rows height gets large with few rows Pin
Anonymous19-Jul-05 2:39
Anonymous19-Jul-05 2:39 
GeneralProblem when placed dropdown control in datagrid Pin
sureshvb18-May-05 20:16
sureshvb18-May-05 20:16 
GeneralRe: Problem when placed dropdown control in datagrid Pin
KjellSJ18-May-05 20:50
KjellSJ18-May-05 20:50 
GeneralRe: Problem when placed dropdown control in datagrid Pin
KjellSJ12-Nov-05 4:21
KjellSJ12-Nov-05 4:21 
GeneralRe: Problem when placed dropdown control in datagrid Pin
TX_Travis14-Mar-06 6:19
TX_Travis14-Mar-06 6:19 
GeneralRe: Problem when placed dropdown control in datagrid Pin
anhph227-Apr-06 22:43
anhph227-Apr-06 22:43 
GeneralRe: Problem when placed dropdown control in datagrid Pin
bguill24-May-06 1:42
bguill24-May-06 1:42 
GeneralRe: Problem when placed dropdown control in datagrid [modified] Pin
RonTrull5-Jun-06 8:46
RonTrull5-Jun-06 8:46 
GeneralRe: Problem when placed dropdown control in datagrid Pin
Your Friend Manoj26-Feb-07 23:20
Your Friend Manoj26-Feb-07 23:20 
QuestionRe: Problem when placed dropdown control in datagrid Pin
sayanghosh28-Feb-07 1:36
sayanghosh28-Feb-07 1:36 
GeneralMulti-row Headers Pin
Wasia29-Apr-05 9:29
Wasia29-Apr-05 9:29 
QuestionPaging turned on- Can you Fix the Pager as well? Pin
SteveMets28-Apr-05 6:34
professionalSteveMets28-Apr-05 6:34 
AnswerRe: Paging turned on- Can you Fix the Pager as well? Pin
KjellSJ28-Apr-05 7:42
KjellSJ28-Apr-05 7:42 
GeneralRe: Paging turned on- Can you Fix the Pager as well? Pin
SteveMets1-May-05 9:25
professionalSteveMets1-May-05 9:25 

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.