Click here to Skip to main content
15,897,334 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have gridview contol in my master page i have bind data in grid view using item templete ,
i have paging enabled on it when i click on print button it prints only current page i want to print all 10 pages at a time how to do this i have used this code to print but its not working can anyone help me .. thanks in advance


C#
public override void VerifyRenderingInServerForm(Control control)
  {
      /*Verifies that the control is rendered */
  }


protected void  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();
}
Posted
Updated 8-Dec-13 23:28pm
v2
Comments
So, what you see in Print preview?
[no name] 14-Dec-13 7:33am    
it returns just blank window on button click

 
Share this answer
 
Comments
[no name] 9-Dec-13 8:05am    
i have gridview in content palce holder
Vishal Pand3y 9-Dec-13 11:22am    
so what ?
[no name] 14-Dec-13 7:34am    
when i click on print button it dosent take any action nor open print wndow
Vishal Pand3y 14-Dec-13 7:39am    
use trigger on that button
 
Share this answer
 
Comments
[no name] 9-Dec-13 5:58am    
its not working at all
[no name] 14-Dec-13 4:10am    
i have a with button but when i click on button it dosent open print window
<asp:button id="Button1" runat="server" onclick="Button1_Click" text="چاپ صفحه جاری" xmlns:asp="#unknown" />

protected void PrintAllPages(object sender, EventArgs e)
        {
            GridView1.AllowPaging = false;
            GridView1.DataBind();
            GridView1.HeaderRow.Cells[9].Visible = false;
            GridView1.FooterRow.Cells[9].Visible = false;
            // Loop through the rows and hide the cell in the first column
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                GridViewRow row = GridView1.Rows[i];
                row.Cells[9].Visible = false;
            }
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            hw.AddStyleAttribute(HtmlTextWriterStyle.Direction, "rtl");
            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();
        }
        protected void PrintCurrentPage(object sender, EventArgs e)
        {
            
            GridView1.PagerSettings.Visible = false;
            GridView1.DataBind();
            GridView1.HeaderRow.Cells[9].Visible = false;
            GridView1.FooterRow.Cells[9].Visible = false;
            // Loop through the rows and hide the cell in the first column
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                GridViewRow row = GridView1.Rows[i];
                row.Cells[9].Visible = false;
            }
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            hw.AddStyleAttribute(HtmlTextWriterStyle.Direction, "rtl");
            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.PagerSettings.Visible = true;
            GridView1.DataBind();
        }
 
Share this answer
 
Comments
Richard Deeming 11-Mar-15 14:52pm    
You've been here nearly five years - long enough to know how we feel about people resurrecting old questions.

This question was posted and solved over a year ago.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900