Answered by:
print gridview with all page with header section

Question
-
User-807418713 posted
Hello
I have one gridview in which i have more then 700 records on asp.net button print i want it show each page with header before taking printing
how to do so using asp.net C#
It would be helpful if any small demo
Thanking You
Wednesday, December 11, 2019 4:25 AM
Answers
-
User409696431 posted
Add styling to your page to hide the extra headers except when printing, and to always do a page break before the headers when printing:
<head runat="server"> <title>Untitled Page</title> <style type="text/css"> .printheader { visibility:hidden; font-size: 0; } tr.printheader td { border: 0px none none; padding:0; margin:0; } @media print { .printheader { page-break-before: always; visibility:visible; font-size: 12pt; font-weight:bold; } tr.printheader td { border: 1px solid black; padding:3px; } } </style> </head>
To your GridView markup, add
OnRowDataBound="GridView1_RowDataBound"
To your code behind, add:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { int breaknumber = 30; if (e.Row.RowType == DataControlRowType.DataRow) { if (IsDivisible(e.Row.RowIndex, breaknumber)) { GridViewRow gvRow = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert); TableCell tCell0 = new TableCell(); tCell0.Text = "ID"; TableCell tCell1 = new TableCell(); tCell1.Text = "NAME"; gvRow.Cells.Add(tCell0); gvRow.Cells.Add(tCell1); gvRow.Attributes.Add("class", "printheader"); Table tbl = e.Row.Parent as Table; tbl.Rows.Add(gvRow); } } } public bool IsDivisible(int x, int n) { if(x < 1) { return false; } return (x % n) == 0; }
Set the integer "breaknumber" to the number of rows that will fit on each page when printing. The code above inserts a header row every "breaknumber" rows, and the styling hides those headers until the page is printed.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, December 11, 2019 11:07 PM -
User288213138 posted
Hi Gopi.MCA,
on print i want ID,Name Headers In All page
how to set?
You can try add below code after bind data to Gridview.
The Grid View. UseAccessibleHeader property get or set a value indicating whether a GridView control renders its header in an accessible format. This property is provided to make the control more accessible to users of assistive technology devices.
GridView1.DataSource = dt; GridView1.DataBind(); GridView1.UseAccessibleHeader = true; GridView1.HeaderRow.TableSection = TableRowSection.TableHeader;
The result:
Best regards,
Sam
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, December 12, 2019 8:22 AM
All replies
-
User288213138 posted
Hi Gopi.MCA,
i want it show each page with header before taking printingAccording to your description, I couldn’t understand your requirement clearly.
Shouldn't Gridview have only one header, why do you want to show each page with header?
So please post more details information about your requirement.
Best regards,
Sam
Wednesday, December 11, 2019 8:26 AM -
User-807418713 posted
Hi
Here is my aspx and c# demo codes
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AABB.aspx.cs" Inherits="AABB" %> <!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>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server" Font-Bold="False" Font-Names="Calibri" Font-Size="12pt"> <Columns> <asp:TemplateField HeaderText="ID"><ItemTemplate> <asp:Label ID="L2" runat="server" Text='<%# Bind("ID") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Name"><ItemTemplate> <asp:Label ID="L2" runat="server" Text='<%# Bind("Name") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div> </form> </body> </html>
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class AABB : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[2] { new DataColumn("ID"), new DataColumn("Name") }); dt.Rows.Add(1, "AA"); dt.Rows.Add(2, "BB"); dt.Rows.Add(3, "CC"); dt.Rows.Add(4, "DD"); dt.Rows.Add(1, "AA"); dt.Rows.Add(2, "BB"); dt.Rows.Add(3, "CC"); dt.Rows.Add(4, "DD"); dt.Rows.Add(1, "AA"); dt.Rows.Add(2, "BB"); dt.Rows.Add(3, "CC"); dt.Rows.Add(4, "DD"); dt.Rows.Add(1, "AA"); dt.Rows.Add(2, "BB"); dt.Rows.Add(3, "CC"); dt.Rows.Add(4, "DD"); dt.Rows.Add(1, "AA"); dt.Rows.Add(2, "BB"); dt.Rows.Add(3, "CC"); dt.Rows.Add(4, "DD"); dt.Rows.Add(1, "AA"); dt.Rows.Add(2, "BB"); dt.Rows.Add(3, "CC"); dt.Rows.Add(4, "DD"); dt.Rows.Add(1, "AA"); dt.Rows.Add(2, "BB"); dt.Rows.Add(3, "CC"); dt.Rows.Add(4, "DD"); dt.Rows.Add(1, "AA"); dt.Rows.Add(2, "BB"); dt.Rows.Add(3, "CC"); dt.Rows.Add(4, "DD"); dt.Rows.Add(1, "AA"); dt.Rows.Add(2, "BB"); dt.Rows.Add(3, "CC"); dt.Rows.Add(4, "DD"); dt.Rows.Add(1, "AA"); dt.Rows.Add(2, "BB"); dt.Rows.Add(3, "CC"); dt.Rows.Add(1, "AA"); dt.Rows.Add(2, "BB"); dt.Rows.Add(3, "CC"); dt.Rows.Add(1, "AA"); dt.Rows.Add(2, "BB"); dt.Rows.Add(3, "CC"); dt.Rows.Add(1, "AA"); dt.Rows.Add(2, "BB"); dt.Rows.Add(3, "CC"); dt.Rows.Add(1, "AA"); dt.Rows.Add(2, "BB"); dt.Rows.Add(3, "CC"); dt.Rows.Add(1, "AA"); dt.Rows.Add(2, "BB"); dt.Rows.Add(3, "CC"); dt.Rows.Add(1, "AA"); dt.Rows.Add(2, "BB"); dt.Rows.Add(3, "CC"); dt.Rows.Add(1, "AA"); dt.Rows.Add(2, "BB"); dt.Rows.Add(3, "CC"); dt.Rows.Add(1, "AA"); dt.Rows.Add(2, "BB"); dt.Rows.Add(3, "CC"); dt.Rows.Add(1, "AA"); dt.Rows.Add(2, "BB"); dt.Rows.Add(3, "CC"); dt.Rows.Add(1, "AA"); dt.Rows.Add(2, "BB"); dt.Rows.Add(3, "CC"); dt.Rows.Add(1, "AA"); dt.Rows.Add(2, "BB"); dt.Rows.Add(3, "CC"); dt.Rows.Add(1, "AA"); dt.Rows.Add(2, "BB"); dt.Rows.Add(3, "CC"); dt.Rows.Add(1, "AA"); dt.Rows.Add(2, "BB"); dt.Rows.Add(3, "CC"); dt.Rows.Add(1, "AA"); dt.Rows.Add(2, "BB"); dt.Rows.Add(3, "CC"); dt.Rows.Add(1, "AA"); dt.Rows.Add(2, "BB"); dt.Rows.Add(3, "CC"); dt.Rows.Add(1, "AA"); dt.Rows.Add(2, "BB"); dt.Rows.Add(3, "CC"); dt.Rows.Add(1, "AA"); dt.Rows.Add(2, "BB"); dt.Rows.Add(3, "CC"); dt.Rows.Add(1, "AA"); dt.Rows.Add(2, "BB"); dt.Rows.Add(3, "CC"); dt.Rows.Add(1, "AA"); dt.Rows.Add(2, "BB"); dt.Rows.Add(3, "CC"); dt.Rows.Add(1, "AA"); dt.Rows.Add(2, "BB"); dt.Rows.Add(3, "CC"); dt.Rows.Add(1, "AA"); dt.Rows.Add(2, "BB"); dt.Rows.Add(3, "CC"); dt.Rows.Add(4, "DD"); dt.Rows.Add(4, "DD"); dt.Rows.Add(4, "DD"); dt.Rows.Add(4, "DD"); dt.Rows.Add(4, "DD"); dt.Rows.Add(4, "DD"); dt.Rows.Add(4, "DD"); dt.Rows.Add(4, "DD"); dt.Rows.Add(4, "DD"); dt.Rows.Add(4, "DD"); dt.Rows.Add(4, "DD"); dt.Rows.Add(4, "DD"); dt.Rows.Add(4, "DD"); dt.Rows.Add(4, "DD"); dt.Rows.Add(4, "DD"); dt.Rows.Add(4, "DD"); dt.Rows.Add(4, "DD"); dt.Rows.Add(4, "DD"); dt.Rows.Add(4, "DD"); dt.Rows.Add(4, "DD"); dt.Rows.Add(4, "DD"); dt.Rows.Add(4, "DD"); GridView1.DataSource = dt; GridView1.DataBind(); } } }
on print i want ID,Name Headers In All page
on print i want ID,Name Headers In All page
how to set?
Thanking You
Wednesday, December 11, 2019 6:45 PM -
User409696431 posted
Add styling to your page to hide the extra headers except when printing, and to always do a page break before the headers when printing:
<head runat="server"> <title>Untitled Page</title> <style type="text/css"> .printheader { visibility:hidden; font-size: 0; } tr.printheader td { border: 0px none none; padding:0; margin:0; } @media print { .printheader { page-break-before: always; visibility:visible; font-size: 12pt; font-weight:bold; } tr.printheader td { border: 1px solid black; padding:3px; } } </style> </head>
To your GridView markup, add
OnRowDataBound="GridView1_RowDataBound"
To your code behind, add:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { int breaknumber = 30; if (e.Row.RowType == DataControlRowType.DataRow) { if (IsDivisible(e.Row.RowIndex, breaknumber)) { GridViewRow gvRow = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert); TableCell tCell0 = new TableCell(); tCell0.Text = "ID"; TableCell tCell1 = new TableCell(); tCell1.Text = "NAME"; gvRow.Cells.Add(tCell0); gvRow.Cells.Add(tCell1); gvRow.Attributes.Add("class", "printheader"); Table tbl = e.Row.Parent as Table; tbl.Rows.Add(gvRow); } } } public bool IsDivisible(int x, int n) { if(x < 1) { return false; } return (x % n) == 0; }
Set the integer "breaknumber" to the number of rows that will fit on each page when printing. The code above inserts a header row every "breaknumber" rows, and the styling hides those headers until the page is printed.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, December 11, 2019 11:07 PM -
User288213138 posted
Hi Gopi.MCA,
on print i want ID,Name Headers In All page
how to set?
You can try add below code after bind data to Gridview.
The Grid View. UseAccessibleHeader property get or set a value indicating whether a GridView control renders its header in an accessible format. This property is provided to make the control more accessible to users of assistive technology devices.
GridView1.DataSource = dt; GridView1.DataBind(); GridView1.UseAccessibleHeader = true; GridView1.HeaderRow.TableSection = TableRowSection.TableHeader;
The result:
Best regards,
Sam
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, December 12, 2019 8:22 AM