locked
Print Datagrid RRS feed

  • Question

  • User-1861919626 posted

    I have a datagrid on a web form, and have googled all over the internet, but every solution I have attempted when I try to print the datagrid only prints a blank page?  What option do you guys recomenend to print a datagrid?

    Tuesday, February 26, 2013 6:50 AM

Answers

  • User44562928 posted

    Hi,

    You can use javascript for print gridview data, Add the following java script in the head section of the page

            function DataPrint()
            {
                var prtContent = document.getElementById('<%= GridView1.ClientID %>');
                prtContent.border = 0; //set no border here
                var Print = window.open('','','left=100,top=100,width=1000,height=1000,toolbar=0,scrollbars=1,status=0,resizable=1');
                Print.document.write(prtContent.outerHTML);
                Print.document.close();
                Print.focus();
                Print.print();
                Print.close();
            }
    
    

    Add asp.net button for Print data like,

     <asp:Button ID="Button1" runat="server" Text="Print" OnClientClick="DataPrint()" />

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 26, 2013 8:09 AM

All replies

  • User44562928 posted

    Hi,

    You can use javascript for print gridview data, Add the following java script in the head section of the page

            function DataPrint()
            {
                var prtContent = document.getElementById('<%= GridView1.ClientID %>');
                prtContent.border = 0; //set no border here
                var Print = window.open('','','left=100,top=100,width=1000,height=1000,toolbar=0,scrollbars=1,status=0,resizable=1');
                Print.document.write(prtContent.outerHTML);
                Print.document.close();
                Print.focus();
                Print.print();
                Print.close();
            }
    
    

    Add asp.net button for Print data like,

     <asp:Button ID="Button1" runat="server" Text="Print" OnClientClick="DataPrint()" />

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 26, 2013 8:09 AM
  • User-1861919626 posted

    It looks like that solution would work, but unfortunately I have to use C# to perform this action.  Apalogies, I should have specified this during my initial post.

    Tuesday, February 26, 2013 9:20 AM
  • User-1034726716 posted

    Are this implemented in windows application?

    Tuesday, February 26, 2013 10:36 PM
  • User1938476581 posted

    Hi,

    You can call javascript in code behind like this

    protected void  PrintCurrentPage(object sender, EventArgs e)
    
    {
    
        GridView1.PagerSettings.Visible = 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.PagerSettings.Visible = true;
    
        GridView1.DataBind();
    
    }
    

    And please refer other samples:

    http://www.aspsnippets.com/articles/print-functionality-in-asp.net-gridview-control.aspx

    http://www.codeproject.com/Articles/28046/Printing-of-DataGridView

    http://www.codeproject.com/Articles/36488/DataGridView-Print-Print-Preview-Solution-Part-I

    Hope it can help you.
     

    Wednesday, February 27, 2013 4:18 AM
  • User-1861919626 posted

    Are this implemented in windows application?

    Implemented via a webform.  Does that answer your question or am I missing something?

    Wednesday, February 27, 2013 10:15 AM
  • User-1034726716 posted

    Implemented via a webform.  

    If it is in webform then is there any reason why you can't use JavaScript? Since you are working with web apps then most likely you will resort to use javascript or client side script to print the html from the page. You can construct your javascript in your code behind or just call a javascript function that handles the print in your code behind as already demonstrated.

    Wednesday, February 27, 2013 10:59 PM