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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css" media="print">
        .hideOnPrint
        {
            display: none;
        }
    </style>
    <style type="text/css">
        @media print
        {
            th
            {
                color: black;
                background-color: white;
            }
            THEAD
            {
                display: table-header-group;
                border: solid 2px black;
            }
        }
    </style>
    <style type="text/css">       
        THEAD
        {
            display: table-header-group;
            border: solid 2px black;
        }
    </style>
</head>
<body onload="javascript:AddTHEAD('<%= GridView1.ClientID %>');">
    <form id="form1" runat="server">
    <div>
        <div class="hideOnPrint">
            <asp:Button ID="Button2" runat="server" OnClientClick="javascript:location.reload(true);window.print();"
                Text="Print" />
        </div>
        <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">
            <Columns>
                <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" />
            </Columns>
            <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" />
        </asp:GridView>
        <br />
        <asp:Literal ID="LClickName" runat="server"></asp:Literal><br />
    </div>
    <script type="text/javascript">
        function AddTHEAD(tableName) {
            var table = document.getElementById(tableName);
            if (table != null) {
                var head = document.createElement("THEAD");
                head.style.display = "table-header-group";
                head.appendChild(table.rows[0]);
                table.insertBefore(head, table.childNodes[0]);
            }
        }
       
    </script>
    </form>
</body>
</html>




The Code Behind

        protected void Page_Load(object sender, EventArgs e)
        {
            bindGV();
        }

        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();
                table.Rows.Add(row);
            }

            GridView1.DataSource = table;
            GridView1.DataBind();
        }

Output Example :

 

Page 1
Page 2
Page 3

Page 4


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


By
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

How to create DataGrid or GridView in JSP - Servlet

Control Webpart Visible/Enable using macro in Kentico