Print Gridview data and display gridview Header on each page? [Resolved]

Posted by Hemanth.Gottipati under ASP.NET on 10/20/2013 | Points: 10 | Views : 13058 | Status : [Member] | Replies : 7
Hi
How to print Grid view data and display grid view header with each page...i am struggled for the solution for past 1 week please give me suggestion how to do that?

after print button click display grid view header on every page?




Responses

Posted by: Bandi on: 10/21/2013 [Member] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
I have tested following code in the Internet Explorer browser..
It will print each 10 records in separate page and also gives column headers for each page
-- Design Page with one GridView names as "grdUserList " and one button named as "Print"

<!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 id="Head1" runat="server">
<style>
@media print
{
input
{
display: none;
}
}
</style>

<title></title>
</head><body>
<form id="form1" runat="server">
<asp:GridView ID="grdUserList" runat="server" AutoGenerateColumns="false" Font-Names="Arial" Font-Size="11pt" AlternatingRowStyle-BackColor="#C2D69B" HeaderStyle-BackColor="green"
AllowPaging="false">
<Columns>
<asp:BoundField ItemStyle-Width="150px" DataField="FullDateAlternateKey" HeaderText="FullDateAlternateKey" />
<asp:BoundField ItemStyle-Width="150px" DataField="EnglishDayNameOfWeek" HeaderText="EnglishDayNameOfWeek" />
<asp:BoundField ItemStyle-Width="150px" DataField="CalendarYear" HeaderText="CalendarYear" />
</Columns>
</asp:GridView><br />
<asp:Button ID="btnPrint" runat="server" Text="Print" OnClick="Print" />
</form>
</body>
</html>

-- Code Behind

protected void Page_Load(object sender, EventArgs e)
{
string connectionstring = "Data source=XXXX; Initial Catalog=AdventureWorksDW2008R2; Integrated Security=true";

SqlConnection conn = new SqlConnection(connectionstring);
conn.Open();
SqlCommand comm = new SqlCommand("SELECT TOP 100 FullDateAlternateKey,EnglishDayNameOfWeek,CalendarYear from [DimDate] ", conn);
SqlDataAdapter da = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
da.Fill(ds);
grdUserList.DataSource = ds;
grdUserList.DataBind();
}
protected void Print(object sender, EventArgs e)
{
grdUserList.UseAccessibleHeader = true;
grdUserList.HeaderRow.TableSection = TableRowSection.TableHeader;
grdUserList.FooterRow.TableSection = TableRowSection.TableFooter;
grdUserList.Attributes["style"] = "border-collapse:separate";
foreach (GridViewRow row in grdUserList.Rows)
{
if (row.RowIndex % 10 == 0 && row.RowIndex != 0)
{
row.Attributes["style"] = "page-break-after:always;";
}
}
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
grdUserList.RenderControl(hw);
string gridHTML = sw.ToString().Replace("\"", "'").Replace(System.Environment.NewLine, "");
StringBuilder sb = new StringBuilder();
sb.Append("<script type = 'text/javascript'>");
sb.Append("window.onload = new function(){");
sb.Append("var printWin = window.open('', '', 'left=0");
sb.Append(",top=0,width=1000,height=600,status=0');");
sb.Append("printWin.document.write(\"");
string style = "<style type = 'text/css'>thead {display:table-header-group;} tfoot{display:table-footer-group;}</style>";
sb.Append(style + gridHTML);
sb.Append("\");");
sb.Append("printWin.document.close();");
sb.Append("printWin.focus();");
sb.Append("printWin.print();");
sb.Append("printWin.close();");
sb.Append("};");
sb.Append("</script>");
ClientScript.RegisterStartupScript(this.GetType(), "GridPrint", sb.ToString());
grdUserList.DataBind();
}

int tempcounter = 0;

protected void grdUserList_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;
}
}
}

public override void VerifyRenderingInServerForm(Control control)
{
/*Verifies that the control is rendered */
}


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Hemanth.Gottipati, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 10/21/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Refer these links
http://www.aspsnippets.com/Articles/Print-ASPNet-GridView-with-page-breaks-and-repeat-header-on-each-page.aspx
http://maheshpashamdotnet.blogspot.in/2012/04/how-to-print-grid-view-in-aspnet-with.html

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Hemanth.Gottipati, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 10/21/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Refer the following link for sample code for printing GridView Header on each page
http://forums.asp.net/t/1896616.aspx

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Hemanth.Gottipati, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hemanth.Gottipati on: 10/21/2013 [Member] Starter | Points: 25

Up
0
Down
I have tried all the above links but its not working properly...printing the gridview working fine but header displayed only on first page..remaining pages not displayed header......

Hemanth.Gottipati, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hemanth.Gottipati on: 10/21/2013 [Member] Starter | Points: 25

Up
0
Down
Thanks a lot..now its working fine..

Hemanth.Gottipati, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 10/21/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
You are welcome :)

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Hemanth.Gottipati, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Longwest on: 7/6/2015 [Member] Starter | Points: 25

Up
0
Down
Thank you a ton! Bandi
The code is of great help.

Best regards,
http://www.keepdynamic.com/

Hemanth.Gottipati, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response