Answered by:
Print both one time gridview1 and gridview2

Question
-
User-807418713 posted
Hello
This is my code
protected void Button4_Click(object sender, EventArgs e) { GridView1.Attributes["style"] = "border-collapse:separate"; foreach (GridViewRow row in GridView1.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); 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()); GridView1.DataBind(); }
I have Gridview1 and Gridview2 on button click i want to print both in same page..
how to do so on my above code
Tuesday, August 4, 2020 8:42 AM
Answers
-
User-1330468790 posted
Hi Gopi.MCA,
The codes are correct and you could simply add GridView2's content to the HtmlTextWriter.
Do you meet any problem implementing the adding operation?
You could refer to below working codes:
.aspx
<form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"></asp:GridView> <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="true"></asp:GridView> </div> <asp:Button ID="PrintBtn" runat="server" Text="Print" OnClick="PrintBtn_Click" /> </form>
Code behind:
// Simulation of the data private static DataTable _gridviewDT; public static DataTable GridviewDT { get { if (_gridviewDT is null) { _gridviewDT = new DataTable(); _gridviewDT.Columns.Add("Id", typeof(int)); _gridviewDT.Columns.Add("Name", typeof(string)); _gridviewDT.Rows.Add(1, "name1"); _gridviewDT.Rows.Add(2, "name2"); _gridviewDT.Rows.Add(3, "name3"); } return _gridviewDT; } set { _gridviewDT = value; } } // Simulation of the data private static DataTable _gridviewDT1; public static DataTable GridviewDT1 { get { if (_gridviewDT1 is null) { _gridviewDT1 = new DataTable(); _gridviewDT1.Columns.Add("Id", typeof(int)); _gridviewDT1.Columns.Add("Name", typeof(string)); _gridviewDT1.Rows.Add(4, "name4"); _gridviewDT1.Rows.Add(5, "name5"); _gridviewDT1.Rows.Add(6, "name6"); } return _gridviewDT1; } set { _gridviewDT = value; } } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindGridView1(); BindGridView2(); } } public override void VerifyRenderingInServerForm(Control control) { //Allows for printing } private void BindGridView1() { GridView1.DataSource = GridviewDT; GridView1.DataBind(); } private void BindGridView2() { GridView2.DataSource = GridviewDT1; GridView2.DataBind(); } protected void PrintBtn_Click(object sender, EventArgs e) { GridView1.Attributes["style"] = "border-collapse:separate"; foreach (GridViewRow row in GridView1.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); GridView1.RenderControl(hw); 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()); }
Demo:
Hope this can help you.
Best regards,
Sean
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, August 5, 2020 9:38 AM