Source code:::: .aspx
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" >
<Columns>
<asp:BoundField DataField="ReadingId" HeaderText="ReadingId" />
<asp:BoundField DataField="date" HeaderText="Date" />
<asp:BoundField DataField="time" HeaderText="Time" />
<asp:BoundField DataField="reading" HeaderText="Reading" />
<asp:BoundField DataField="unitType" HeaderText="UnitType" />
<asp:BoundField DataField="comment" HeaderText="Comment" />
<asp:BoundField DataField="MealTime" HeaderText="MealTime" />
</Columns>
</asp:GridView>
Code behind::: .aspx.cs
protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
{
GridView2.Visible = true;
GridView2.UseAccessibleHeader = true;
GridView2.HeaderRow.TableSection = TableRowSection.TableHeader;
GridView2.FooterRow.TableSection = TableRowSection.TableFooter;
GridView2.Attributes["style"] = "border-collapse:separate";
foreach (GridViewRow row in GridView2.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);
GridView2.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());
GridView2.AllowPaging = true;
GridView2.DataBind();
GridView2.Visible = false;
}
public override void VerifyRenderingInServerForm(Control control)
{
/*Verifies that the control is rendered */
}
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" >
<Columns>
<asp:BoundField DataField="ReadingId" HeaderText="ReadingId" />
<asp:BoundField DataField="date" HeaderText="Date" />
<asp:BoundField DataField="time" HeaderText="Time" />
<asp:BoundField DataField="reading" HeaderText="Reading" />
<asp:BoundField DataField="unitType" HeaderText="UnitType" />
<asp:BoundField DataField="comment" HeaderText="Comment" />
<asp:BoundField DataField="MealTime" HeaderText="MealTime" />
</Columns>
</asp:GridView>
<asp:ImageButton ID="ImageButton3" Width="100px" Height="70px" ImageUrl="~/images/print.jpg"
runat="server" OnClick="ImageButton3_Click" />
protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
{
GridView2.Visible = true;
GridView2.UseAccessibleHeader = true;
GridView2.HeaderRow.TableSection = TableRowSection.TableHeader;
GridView2.FooterRow.TableSection = TableRowSection.TableFooter;
GridView2.Attributes["style"] = "border-collapse:separate";
foreach (GridViewRow row in GridView2.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);
GridView2.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());
GridView2.AllowPaging = true;
GridView2.DataBind();
GridView2.Visible = false;
}
public override void VerifyRenderingInServerForm(Control control)
{
/*Verifies that the control is rendered */
}
GridView data printing using C# code
ReplyDeleteSorry for not reply, I did not see your comment. Please reach me if you have any queries
Deletenagireddygajjela19@gmail.com
its nt wrking error in String writer
Delete