Click here to Skip to main content
11,790,613 members (52,373 online)
Click here to Skip to main content

Fixed header in ASP.NET DataGrid

, 20 Apr 2005 350.4K 92
Rate this:
Please Sign up or sign in to vote.
How to get a fixed header row in an ASP.NET DataGrid?


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: 
<asp:DataGrid ...

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);}

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.


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

Web Developer
Norway Norway
My blog:
My contribution to other stuff at CodeProject:

You may also be interested in...

Comments and Discussions

QuestionCan I also alter the color of Visited Link in the datagrid header? Pin
smcirish22-Apr-08 8:09
membersmcirish22-Apr-08 8:09 
AnswerRe: Can I also alter the color of Visited Link in the datagrid header? Pin
nsimeonov5-Dec-08 14:55
membernsimeonov5-Dec-08 14:55 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1509028.1 | Last Updated 21 Apr 2005
Article Copyright 2005 by KjellSJ
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid