Thursday 28 January 2016

printing all pages with paging in gridview in asp.net using c#

In Print button Click,

Add the following Code ,Here I want to Print GridView1 Data without Paging but  visually Paged Grid.
So Here First Remove Paging and Bind Grid then apply Javascript  for Print Preview PopUp then again Apply Paging in Gridview.


 PrintAllPages(object sender, EventArgs e)  
  {  
   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=1000,height=600,status=0');");  
   sb.Append("printWin.document.write(\"");  
   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();  
  }