Post date: Nov 14, 2009 12:16:5 AM
Printing the data from GridView is one of the desirable feature.
Now you can print single page or all the pages available in the grid. The code snippet below shows how to print GridView data.
Printing the Current Page
Line 1: gvReport.DataSource = (DataSet)Session["ReportData"];
gvReport.PagerSettings.Visible = false;
gvReport.DataBind();
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gridview1.RenderControl(hw);
string gridHTML = sw.ToString().Replace("\"", "'").Replace(System.Environment.NewLine, "");
StringBuilder sb = new StringBuilder();
sb.Append("<script type = 'text/javascript'>");
Line 10: sb.Append("window.onload = new function(){");
sb.Append("var printWin = window.open('', '', 'left=0");
sb.Append(",top=0,width=800,height=600,status=0');");
sb.Append("printWin.document.write(\"");
sb.Append("<div style='float:right;font-size:11px'>Report Generated on :" + DateTime.Now.ToString("dd-MM-yyyy")+ "</div>");
sb.Append("<div style='float:left;font-size:15px;font-weight:bold;'>" + "Report Name....." + "</div>");
sb.Append("<br/><br/>");
sb.Append(gridHTML);
Line 20: 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());
gvReport.PagerSettings.Visible = true;
Line 30: gvReport.DataBind();
Explanation:
Line 1: "ds" is the dataset containing the data retrieved on the form, you can either retrieve the data again or save the data retrieved in the Grid (at first instance) into session and assign the session data instead of ds
Line 14: the text "Report Generated on : dd/MM/yyyy" appears on the top-right corner of the report. You can replace this text whatever you want
Line 15: the text "Report Name....." can be anything which you want to appear in the report header on the left. Either you can place a textbox on the page and let user enter the report name of his/her choice or let the name be static
Printing all the pages of the Grid
gridview1.DataSource = ds;
gridview1.AllowPaging = false;
gridview1.DataBind();
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gridview1.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=800,height=600,status=0');");
sb.Append("printWin.document.write(\"");
sb.Append("<div style='float:right;font-size:11px'>Heading on right corner</div>");
sb.Append("<div style='float:left;font-size:15px;font-weight:bold;'>" + "report name" + "</div>");
sb.Append("<br/>");
sb.Append(gridHTML);
sb.Append("\");");
sb.Append("printWin.document.close();");
sb.Append("printWin.focus();");
sb.Append("printWin.print();");
sb.Append("printWin.close();};");
sb.Append("</script>");
ClientScript.RegisterStartupScript(this.GetType(), "GridPrint", sb.ToString());
gridview1.AllowPaging = true;
gridview1.DataBind();
The only difference between single page and all page printing codes is, the paging is dissabled so that all the pages can be printed.
You may also need to include below code in your codebehind.
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
If this code is not added in the codebehind, then you may get an error the
Control 'gridview1' of type 'GridView' must be placed inside a form tag with runat=server.
the above exception occurs when one tries to export a gridview control to word, pdf or any other formats. the .Net compiler thinks that the control is not added to the form and is being rendered, hence it throws this error even if the gridview control is placed inside the form tag with runat="server"