Hi maniks,
I have changed your code and wroking correctly with no error. Check the below code.
HTML
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" Font-Names="Arial"
Font-Size="11pt" AlternatingRowStyle-BackColor="#C2D69B" HeaderStyle-BackColor="green"
AllowPaging="false">
<Columns>
<asp:BoundField ItemStyle-Width="50px" DataField="ENO" HeaderText="ENO" />
<asp:BoundField ItemStyle-Width="150px" DataField="ENAME" HeaderText="ENAME" />
<asp:BoundField ItemStyle-Width="150px" DataField="JOB" HeaderText="JOB" />
</Columns>
</asp:GridView>
</div>
<br />
<asp:Button ID="btnPrint" runat="server" Text="Print" OnClick="Gridprint" />
Code
protected void Page_Load(object sender, EventArgs e)
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
BindGridview();
}
protected void BindGridview()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("ENO", typeof(int)),
new DataColumn("ENAME", typeof(string)),
new DataColumn("JOB", typeof(string)) });
dt.Rows.Add(1, "Mudassar Khan", "Owner");
dt.Rows.Add(2, "Maria ", "Sales Representative");
dt.Rows.Add(3, "Ana Trujillo ", "Owner");
dt.Rows.Add(4, "Antonio Moreno ", "Owner");
dt.Rows.Add(5, "Thomas Hardy ", "Sales Representative");
dt.Rows.Add(6, "Christina Berglund ", "Order Administrator");
dt.Rows.Add(7, "Hanna Moos ", "Sales Representative");
dt.Rows.Add(8, "Frédérique Citeaux ", "Marketing Manager");
dt.Rows.Add(9, "Martín Sommer ", "Owner");
dt.Rows.Add(10, "Laurence Lebihan ", "Owner");
dt.Rows.Add(11, "Elizabeth Lincoln", "Accounting Manager");
dt.Rows.Add(12, "Victoria Ashworth", "Sales Representative");
dt.Rows.Add(13, "Patricio Simpson", "Sales Agent");
GridView1.DataSource = dt;
GridView1.DataBind();
}
public override void VerifyRenderingInServerForm(Control control)
{
/*Verifies that the control is rendered */
}
protected void Gridprint(object sender, EventArgs e)
{
GridView1.UseAccessibleHeader = true;
GridView1.HeaderRow.TableSection = TableRowSection.TableHeader;
GridView1.FooterRow.TableSection = TableRowSection.TableFooter;
GridView1.Attributes["style"] = "border-collapse:separate";
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowIndex % 9 == 0 && row.RowIndex != 0)
{
row.Attributes["style"] = "page-break-after:always;";
}
}
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=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());
BindGridview();
}
Screenshot