Print GridView Header On Each Page While On Printing : ASP.NET

This example will show how you can include Table header on each page when you print the html document. You need to have the css, and javascript to do this objective. Let see the full example.

The ASPX Page

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
    <style type="text/css" media="print">
            display: none;
    <style type="text/css">
        @media print
                color: black;
                background-color: white;
                display: table-header-group;
                border: solid 2px black;
    <style type="text/css">       
            display: table-header-group;
            border: solid 2px black;
<body onload="javascript:AddTHEAD('<%= GridView1.ClientID %>');">
    <form id="form1" runat="server">
        <div class="hideOnPrint">
            <asp:Button ID="Button2" runat="server" OnClientClick="javascript:location.reload(true);window.print();"
                Text="Print" />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White"
            BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black"
            GridLines="Vertical" Font-Names="Arial" Font-Size="12px" OnRowCommand="ActionCommand">
                <asp:BoundField DataField="ID" HeaderText="NO." />
                <asp:BoundField DataField="CustomerName" HeaderText="Customer Name" />
                <asp:ButtonField CommandName="showName" Text="Show Name" HeaderText="Show Name" ShowHeader="True" />
                <asp:ButtonField CommandName="EditName" Text="EditName" HeaderText="Edit Name" />
            <FooterStyle BackColor="#CCCCCC" />
            <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="#CCCCCC" />
        <br />
        <asp:Literal ID="LClickName" runat="server"></asp:Literal><br />
    <script type="text/javascript">
        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 Code Behind

        protected void Page_Load(object sender, EventArgs e)

        private void bindGV()
            DataTable table = new DataTable();
            table.Columns.Add("ID", typeof(string));
            table.Columns.Add("CustomerName", typeof(string));

            for (int i = 0; i < 100; i++)
                DataRow row = table.NewRow();
                row["ID"] = i.ToString();
                row["CustomerName"] = "customer Name: " + i.ToString();

            GridView1.DataSource = table;

Output Example :


Page 1
Page 2
Page 3

Page 4

Note : The above solution already tested on Mozilla Firefox and IE only.

NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)

Popular posts from this blog

Example to disable save as certain file type in SSRS Report Viewer

Phone book application example - Java

Modal Popup Message Box ASP.NEt C# Example