Is it possible to have the page break after every records changes in the rows while printing gridview using C#.
please advise..
tried,
- protected void Print(object sender, EventArgs e)
- {
- GridView2.UseAccessibleHeader = true;
- GridView2.HeaderRow.TableSection = TableRowSection.TableHeader;
- GridView2.FooterRow.TableSection = TableRowSection.TableFooter;
- GridView2.Attributes["style"] = "border-collapse:separate";
- string preffered_name = "";
- foreach (GridViewRow row in GridView2.Rows)
- {
- String grid_dept = row.Cells[2].Text;
- if (string.IsNullOrEmpty(preffered_name))
- preffered_name = grid_dept;
- if (preffered_name != grid_dept)
- {
- 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=3000,height=600,status=0');");
- sb.Append("printWin.document.write(\"");
- string style = "thead {display:table-header-group;} tfoot{display:table-footer-group;}";
- 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.DataBind();preffered_name
- preffered_name = grid_dept;
- }
- }
- }
- int tempcounter = 0;
- protected void GridView2_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 */
- }
Bikesh SrivastavaPosted Oct 28, 2016, 8:04 AM
Manas MohapatraPosted Oct 28, 2016, 6:22 AM