Click here to Skip to main content
12,886,913 members (36,678 online)
Click here to Skip to main content
Add your own
alternative version


60 bookmarked
Posted 1 Aug 2006

How to include a header on each page when printing a DataGrid

, 1 Aug 2006
Rate this:
Please Sign up or sign in to vote.
An article on printing issues of DataGrid
<!-- -->

No header on second page

Header is there on second page


In many situations we might use a DataGrid for reporting purposes. If the report contains many pages we will face the problem of the header only appearing on the first page, and not printing on all pages. With a little JavaScript and CSS we can easily solve this issue.

Using the code

A DataGrid will be rendered as a table element.

If you apply the following CSS rule to THEAD elements:

  display : table-header-group;

then everything in a THEAD tag will be printed on every page. However, the DataGrid will not render a THEAD. So the above style will not work. We can add a THEAD to the table(rendered by the DataGrid) with the following JavaScript code.

function AddTHEAD(tableName)
   var table = document.getElementById(tableName); 
   if(table != null) 
    var head = document.createElement("THEAD"); = "table-header-group";
    table.insertBefore(head, table.childNodes[0]); 

The parameter ‘tableName’ is the ID of the datagrid. Calling this function from Onload will work.

<body onload="javascript: AddTHEAD('DataGrid')">

The function create a THEAD tag and add the first row of the table (header) to it. If the header consists of more than one row you need to add the necessary rows to the created THEAD.


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

Abhilash Nedumpurath
United States United States
I have 3 years of IT experience.
Doing : C#, ASP.NET,VB,SQL Server, Oracle

Currently working in Technopark, Kerala,India

You may also be interested in...


Comments and Discussions

QuestionCan We have record numbers on the header or any place on page print preview. Like 1-25, 25-30 for 30 records Pin
rohit kakria7-Apr-13 22:36
memberrohit kakria7-Apr-13 22:36 
GeneralMy vote of 5 Pin
prasan kumar19-Oct-12 0:20
memberprasan kumar19-Oct-12 0:20 
QuestionVery Good... Help me a lot.. Pin
sibiraj91727-Jun-12 0:57
membersibiraj91727-Jun-12 0:57 
GeneralMy vote of 1 Pin
Mohammed Al-Kharouf7-Mar-10 13:29
memberMohammed Al-Kharouf7-Mar-10 13:29 
GeneralThank you! Pin
shravaniraj29-Jun-09 2:51
membershravaniraj29-Jun-09 2:51 
GeneralDatagrid and Heading printing Pin
prabodh1126-Aug-08 20:45
memberprabodh1126-Aug-08 20:45 
GeneralThanks Pin
khamis26-Jul-08 5:41
memberkhamis26-Jul-08 5:41 
Questionunable to get the upper line of the header on the second page onwards Pin
Member 188282127-May-08 3:23
memberMember 188282127-May-08 3:23 
GeneralSimple way to do that Pin
samir4118027-Sep-07 23:59
membersamir4118027-Sep-07 23:59 
You don't have to code so much for doing the above task;P
there is an easy way around

step 1) Add this is a fragment of VB.NET(PS: convert into C# if you like) code on page_load event after binding the datagrid to a datasource

If DataGrid1.Rows.Count > 0 Then DataGrid1.HeaderRow.TableSection = TableRowSection.TableHeader

the above line of code will render the <THEAD> element in ASP.NET 2.0 datagrid

step 2) add a <thead> element to CSS and set its display style to "table-header-group"
Add the following lines of code in CSS
<br />
thead<br />

EnnnnnnnnnnjoyLaugh | :laugh:

GeneralRe: Simple way to do that Pin
shravaniraj29-Jun-09 2:41
membershravaniraj29-Jun-09 2:41 
Generalthanks Pin
chathuraka19-Aug-07 19:15
memberchathuraka19-Aug-07 19:15 
GeneralRe: thanks Pin
Abhilash Nedumpurath21-Aug-07 1:11
memberAbhilash Nedumpurath21-Aug-07 1:11 
Questioni am not getting the uper line of the header on page second onwards? Pin
vsuneeldot17-Jan-07 20:00
membervsuneeldot17-Jan-07 20:00 
AnswerRe: i am not getting the uper line of the header on page second onwards? Pin
Mahendiran28-Aug-07 2:49
memberMahendiran28-Aug-07 2:49 
Generalty so much Pin
John Secada14-Sep-06 9:29
memberJohn Secada14-Sep-06 9:29 
GeneralRe: ty so much Pin
Abhilash Nedumpurath21-Aug-07 1:11
memberAbhilash Nedumpurath21-Aug-07 1:11 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 1 Aug 2006
Article Copyright 2006 by Abhilash Nedumpurath
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid