Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Gridview Control have more then 100 rows of data, which is displayed in six to seven pages.
how i can Print ASP.Net GridView with page breaks and repeat header to each page.

actually i have some code which work on Internet Explorer 11.0.9600 [Windows 8.1]
but not on Google Chrome means it is not cross browser.

What changes i can do so that i will work on chrome and another browser also ?



printpage.aspx

XML
<head>
    <title>Print Monthly Society Bill</title>
    <link href="../Styles/bootstrap.min.css" rel="stylesheet" />
    <link href="../Styles/Style.css" rel="stylesheet" />

    <script src="../Scripts/jquery-2.1.0.min.js"></script>
    <script src="../Scripts/bootstrap.min.js"></script>
    <script type="text/javascript">
        function PrintPanel() {
            var panel = document.getElementById("<%=PrintPanel.ClientID %>");
            var printWindow = window.open('', '', 'height=660,width=1350');
            printWindow.document.write('<html><head><title ></title>');
            printWindow.document.write('</head><body class="container" >');
            printWindow.document.write('<div style="text-align:left">Soceity Care Point<br />societycarepoint@gmail.com</div>');
            printWindow.document.write('<div style="text-align:center"><b>Ashtam Apartment Society Bill</b></div>');
            printWindow.document.write('<style type = "text/css">thead {display:table-header-group;} tfoot{display:table-footer-group;}</style>');
            printWindow.document.write(panel.innerHTML);
            printWindow.document.write('</body></html>');
            printWindow.document.close();
            setTimeout(function () {
                printWindow.print();
            }, 500);
            return false;
        }
    </script>

</head>



XML
<body>
    <form id="form1" runat="server">
        <h1 style="text-align: center">Welcome on Printing Bill of your App</h1>

        <div class="row">
            <div class="container col-md-5">
                <asp:DropDownList ID="ddlsocietyname" CssClass="form-control" runat="server"></asp:DropDownList>
            </div>
            <div class="container col-md-5">
                <asp:Button ID="btnGEnerateBill" CssClass="btn btn-success" runat="server" Text="Generate Bill" OnClientClick="return PrintPanel()" OnClick="btnGEnerateBill_Click" />
            </div>
        </div>
    </form>
</body>




Code-Behind page of printpage.aspx

C#
protected void Page_Load(object sender, EventArgs e)
       {
           gvMonthlyBill.UseAccessibleHeader = true;
           gvMonthlyBill.HeaderRow.TableSection = TableRowSection.TableHeader;
       }



C#
int tempcounter ;
       protected void gvMonthlyBill_RowDataBound(object sender, GridViewRowEventArgs e)
       {
           if (e.Row.RowType == DataControlRowType.DataRow)
           {
               tempcounter = tempcounter + 1;
               if (tempcounter == 10)
               {
                   e.Row.Attributes.Add("style", "page-break-after: always;");
                   tempcounter = 0;
               }
           }
       }
Posted
Comments
Not clear. What is the problem with this code?
Tarun Jaiswal 2-Jun-14 3:28am    
my problem is that i m trying to print panel content on button click, when content is ready for printing i want header of Gridview to be printed on all page so that user don't have turn page again and again for searching what is this column name and also a page breaker at the end of the page so that content of gridview don't get cut in middle.

Now i m able to achieve this function with the above code in Internet Explore Browser but this exact code don't work in Chrome Browser why ? i dont know ....
Manoj B. Kalla 21-Jun-14 4:39am    
I suggest you should go through pure C# code, with help of C# loop and create your own Header method and detail method (line by line print method) in HTML type of code.

eg.
foreach(gridviewrow row in gridview.rows)
{
response.write("<tr>"+row['FirstName']+"<tr>");
}

thisway you can achieve.

Print it this doucment like PRINT TO FILE format.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900