GridView Data Print In Asp.net 2.0

here is the steps:

1) create simple web application (here i used asp.net with C#).

2) put following control in to Page.

1) Gridview (id = GridView1)

2) html button (id =Button1)

<inputid=”Button1″type=”button”value=”Print”language=”javascript”onclick=”return Button1_onclick()” />
<
asp:GridViewID=”GridView1″runat
=”server”>
</
asp:GridView>

3)  fill data into GridView

//here i used the list you  can bind with database also
System.Collections.Generic.List<string> obj = newSystem.Collections.Generic.List<string>();

for(inti = 0; i < 10; i++)
{
obj.Add(i.ToString());
}
GridView1.DataSource = obj;
GridView1.DataBind();
4) now write the print function. click on the Button1 or write Java Script.

<script language="javascript" type="text/javascript">
<!--
function Button1_onclick() {
//open new window set the height and width =0,set windows position at bottom
var a = window.open ('','','left =' + screen.width + ',top=' + screen.height + ',width=0,height=0,toolbar=0,scrollbars=0,status=0');
//write gridview data into newly open window
a.document.write(document.getElementById('<%= GridView1.ClientID %>').innerHTML);
a.document.close();
a.focus();
//call print
a.print();
a.close();
return false;
}
// -->
</script>

Thanks

Advertisements

75 comments

  1. hi Amit,

    your solution was working. But The data in the grid is printed like text and not like table. Is there any way to print the data as table.

    Regards,
    Abhi

    1. jyothi · · Reply

      hello sir,,
      can u clear my doubt??
      i am printing gridview, its working fine…
      but the problem is that when i am printing the data of gridview,it is not in proper format.
      its showing output as paragragh.
      thanx in advance..
      plz reply me back as soon as possible,,

  2. hi Abhiram,

    you just little bit change in the code. put the GridView Inside the Div.
    as shown below

    .
    after above change now change in the javascript which describe below
    a.document.write(document.getElementById(‘tbl’).innerHTML);

    try above code.

    Regards,
    Amit S Patriwala.

    1. Hi, patriwala
      I put my gridview inside div but it will not write data to document.
      My code is shown below>>>>
      function Button1_onclick() {
      //open new window set the height and width =0,set windows position at bottom
      var a = window.open(”, ”, ‘left =’ + screen.width + ‘,top=’ + screen.height + ‘,width=0,height=0,toolbar=0,scrollbars=0,status=0’);
      //write gridview data into newly open window
      a.document.write(‘DROGARIA COLVALCAR’);
      a.document.write(‘ Sunny’);
      a.document.write(document.getElementById(‘divInnerData’).innerHTML);
      a.document.write(‘ Sunny’);
      a.document.close();
      a.focus();
      //call print
      a.print();
      a.close();
      return false;
      }

      & Gridview Code is

      please help me why it’s not working????

    2. Hi, patriwala
      I put my gridview inside div but it will not write data to document.
      My code is shown below>>>>
      function Button1_onclick() {
      //open new window set the height and width =0,set windows position at bottom
      var a = window.open(”, ”, ‘left =’ + screen.width + ‘,top=’ + screen.height + ‘,width=0,height=0,toolbar=0,scrollbars=0,status=0’);
      //write gridview data into newly open window

      a.document.write(‘ Sunny’);
      a.document.write(document.getElementById(‘divInnerData’).innerHTML);
      a.document.write(‘ Sunny’);
      a.document.close();
      a.focus();
      //call print
      a.print();
      a.close();
      return false;
      }

      & Gridview Code is

      please help me why it’s not working????
      It writes data i had written at this line>>>
      a.document.write(‘ Sunny’);

  3. sorry but could you please tell me where to write a.document.write(document.getElementById(‘tbl’).innerHTML; or you mean instead of something

  4. hello Mohamed,
    you just change in the above javascript code
    a.document.write(document.getElementById(”).innerHTML); change that and write a.document.write(document.getElementById(’tbl’).innerHTML;

    Have a Nice day

  5. hi patriwala
    sorry but when i change it, the button gives me error and dosen’t open the print window
    here is the code :

    <!–

    function Button1_onclick() {

    var a = window.open (”,”,’left =’ + screen.width + ‘,top=’ + screen.height + ‘,width=0,height=0,toolbar=0,scrollbars=0,status=0’);

    a.document.write(document.getElementById(”).innerHTML);

    a.document.close();
    a.focus();

    a.print();
    a.close();

    return false;

    }

    and I changed the line as you said with the new line , but it gives me an error when i click the button….

  6. and please in 8 August you said “put the GridView inside the div as shown below ” but nothin appear below , could you please repeat writing it again as you written the code in first time

  7. hello Mohamed,
    i give modified code please check it.

    // in aspx file
    put the gridview inside the div (id=innerData)
    //in javascript
    function Button1_onclick() {
    //open new window set the height and width =0,set windows position at bottom
    var a = window.open ('','','left =' + screen.width + ',top=' + screen.height + ',width=0,height=0,toolbar=0,scrollbars=0,status=0');
    //write gridview data into newly open window

    //major change here get innerHTML Of the Div

    a.document.write(document.getElementById('innerData').innerHTML);
    a.document.close();
    a.focus();
    //call print
    a.print();
    a.close();
    return false;
    }

    thank you.

  8. Many thanx , it works as I want exactly, sorry for disturbance…

  9. put the gridview inside the div (id=innerData)

    I dont understand :$

    it gives error ?

    can anyone write the code ?

  10. hello tcatu,

    i give modified code please check it.

    // in aspx file
    put the gridview inside the div (id=innerData)
    //in javascript
    function Button1_onclick() {
    //open new window set the height and width =0,set windows position at bottom
    var a = window.open (”,”,’left =’ + screen.width + ‘,top=’ + screen.height + ‘,width=0,height=0,toolbar=0,scrollbars=0,status=0’);
    //write gridview data into newly open window

    //major change here get innerHTML Of the Div

    a.document.write(document.getElementById(’innerData’).innerHTML);
    a.document.close();
    a.focus();
    //call print
    a.print();
    a.close();
    return false;
    }

    thank you.

  11. hi Patriwala,

    your solution was working.

    But The data in the Div is printed like text is big.
    How is reduce in font size for print only.

    Regards,
    Priya

  12. hi,
    you can assign font-size in to div.
    in the div put below style.
    style=”font-size:10px;”
    good work

    check this i think it work.

    thnx

    1. V.R.Hari · · Reply

      the font specs in the div is not working….any other solution pl…?

  13. Thanks Patriwala,

    I want to give print out of the records which was in div.
    . When click the print button ,It goes for print out directly.What my problem is,In IE,only the print dialog opened.Its working perfectly.
    But In Firefox,Another one page opens along the print modal dialogbox.

    How is handle this problem.
    Again Thank to you,

    Regards,
    Priya.

    1. hi Priya,
      i got your problem. in the firefox height is issue. please try below code.

      var a = window.open (”,”,’left =’ + screen.width + ‘,top=’ + screen.height + ‘,width=0px,height=1px,toolbar=0,scrollbars=0,status=0,resizable=no’);

      in this code i gave height =1px.

      popup is reside the print dialog box.

      thnx

      1. Hi,
        I am getting error as Invalid argument in IE 6.0 ,But while using the below code in Firefox it’s working fine.Pls provide me a solution to work in IE also.

        var a = window.open (”,”,’left =’ + screen.width + ‘,top=’ + screen.height + ‘,width=0px,height=1px,toolbar=0,scrollbars=0,status=0,resizable=no’);

        Thanks in advance

        Regards,
        manoj

  14. Thanks you Patriwala,

    I changed with and height =1px.

    Very nice.

    again thanks.
    Regards Priya.

    1. most welcome Priya

      thnx

  15. hi,

    What if my grid has 100 rows and page size is 10 then does it will print all the pages i.e. 100 rows or just current page?

    Thanks
    Parag

    1. hi parag,
      it print current page record.

      thnx

  16. hello patriwala,
    I want the grid heading and logo with print.how can i do that with current grid view print code?

    Image

    With Thanks,
    Vinit

    1. hi vinit,
      you can print grid heading with logo.
      you can simply add Template Field for that. In Template Field you can add HeaderTemplate and ItemTemplate.

      thnx

  17. hello patriwala,
    I want to give a Heading and a logo in the page .can i do this?

    1. yes vinit,
      you can do that. you can simple add table in the Header Template. ex:
      HeaderTemplate
      Table
      Tr
      Td
      HeaderName
      Header Image
      end Td
      end Tr
      end Table
      end HeaderTemplate

      check above code.

      thnx

  18. hi Patriwala,

    your solution was working, but only for the records in the current page.
    i want to print all the records in the gridview.
    Please send me the code to print all the records in the gridview…

    Thank you
    Himaja.

    1. hi Himaja,
      yes you are right this code is working for current page. i have suggest one way to print all the record on page. you do one save all data in the dataset (create one property of that like MyPropertyDS in that you use the viewstate). now when you print data at that time read from this property.

      if any doubt then reply to me. i have do that asap.

      thnx

      1. I want to know how to print multiple pages?
        I want to Add Header to this printing Document. For ex. Company Name & Address

  19. hi Patriwala,

    thank you for ur reply, but i dint get exactly what to do.
    (if we do this way,then no need of the above javascript???)

    iam keeping the dataset in sessions .
    when and where shall i call this ? ( in printButton_click event) and how?

    Please send me the code how to print all the records if gridview has paging in C#.

    Thank You
    Himaja

  20. Hi Patriwala ,
    I want to print names taken from dataTable on dot matrix printer in the formated way in asp. net & Vb.net 2.0

    Eg
    Manish

    Nilesh

    Sandeep

    And ther may be more than one page …
    Could you help me please …

  21. Williams · · Reply

    pls, can you explain to me how to print the content of a table in asp net.

    1. hi Williams,
      you can get the tableid in the javascript. here i used the gridviewid you can simple replace with the tableid. check out that thing.

      thnx

  22. Hi Patriwala,
    I tried your code but is not working for me in asp.net (VB). see my code below. the window to print from does not come up at all. pls, help me resolve the issue and send the correct code immediately. Thanks. God bless you.

    function print_grid(){
    var a =window.open(“,”,’left =’ + screen.width + ‘,top=’ + screen.height + ‘,width=0,height=0,toolbar=0,scrollbars=0,status=0’);
    // a.document.write(document.getElementById(’innerData’).innerHTML);
    a.document.write(document.getElementById(‘innerData’).innerHTML);
    a.document.close();
    a.focus();
    a.print();
    a.close();
    return false;

  23. Hi Patriwala,

    I used your code. its working fine. Thanks for your efforts but I want to know about if I want to print all the records in grid view. i tried to implement your solution which you told above in your post but I can’t able to implement it successfully.

    If you provide some code or explain a bit more. It will be more helpful

    Regards
    Adeel Arshad

  24. thanks i think it was perfect and its working perfectly

  25. There is a bug in your code. If the record in Grid has more Number of columns, then all the columns are not coming in print area. Print is accomadating only few columns and chopping other columns without printing. I am looking for solution from many days, but i am not able to fix it. Finally refering to you. Can you please help with this issue.

  26. hi
    i tried the given code: but it is not printing in tabuler formate plz tell how to do it.

    function aa()
    {
    var a = window.open (”,”,’left =’ + screen.width + ‘,top=’ + screen.height + ‘,width=0,height=0,toolbar=0,scrollbars=0,status=0’);
    a.document.write(document.getElementById(”).innerHTML);
    a.document.close();
    a.focus();
    //call print
    a.print();
    a.close();
    return false;

    }

  27. I have tried to incorporate your code but when I click on the print button, nothing happen..
    Here are my codes:

    Public cur_ds As DataSet

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim C_str As String = “Data Source=localHost;Initial Catalog=adventureWorks;Integrated Security=True”
    Dim conn As SqlConnection = New SqlConnection(C_str)
    Dim sql As String = “select contactID,FirstName,LastName from Person.Contact”
    Dim da As New SqlDataAdapter
    da.SelectCommand = New SqlCommand(sql, conn)
    Dim ds As New DataSet
    da.Fill(ds, “contact”)
    GV_Rec.DataSource = ds
    GV_Rec.DataBind()
    cur_ds = ds
    End Sub

    function bt_print_onclick(){
    var a = window.open (”,”,’left =’ + screen.width + ‘,top=’ + screen.height + ‘,width=0,height=0,toolbar=0,scrollbars=0,status=0’);
    //write gridview data into newly open window
    a.document.write(document.getElementById(”).innerHTML);
    a.document.close();
    a.focus();
    //call print
    a.print();
    a.close();
    return false;

    }

    Untitled Page

  28. Hi Patriwala,

    I used your code. its working fine. Thanks for your efforts but I want to know that how can I change the page setup.

    Regards
    Gul

  29. Suresh A · · Reply

    Hi how to implement the same when there are more pages. I mean say i have 15 pages of content and i want to print the all the pages on a button click…

  30. […] Click here to read more….. […]

  31. SHOWIN LOVE,

  32. Hummina Hummina huh! Keep up the good work!!,

  33. Just a guy trying to make the world a better place.,

  34. Just a guy trying to make the world a better place.,

  35. Hi
    The code doesnt seem to work in VB. The Print option window doesnt come up, with the code you have given. anything different in VB..
    Thanx..

  36. Hey very nice blog!!….I’m an instant fan, I have bookmarked you and I’ll be checking back on a regular….See ya

  37. Super-Duper site! I am loving it!! Will come back again – taking your feeds too now, Thanks.

  38. Nice work, thank you. . . .

  39. how to repeat gridview header on new page

  40. I used your code. its working fine.
    how to repeat gridview header on new page

  41. Alok Tripathi · · Reply

    Hi Amit, Thanks for printing code. Its working fine, but i want to print 40 columns and 1000 records. What will be solution.. Thanks in advance.

  42. Hi all,

    I have implemented the solution and it works fine.

    I will ask for one more addition, in my gridview i have 4 boundcolumns and 1 hyperlink column. While printing i want to print those 4 boundcolumns and not the 5th one.

    How can we done this by javascript?

  43. hi patriwala,
    above code works fine, i have 1000 records in grid and i want to print 10 records on every page and its working but my problem is that i can not print 10 records in tabel .
    t

  44. I don’t usually reply to posts but I will in this case, great info…I will add a backlink and bookmark your site. Keep up the good work!

    I’m Out! 🙂

  45. Hi sir

    i tried your code but it is generating this error under:-
    using VS2005 .net ver 2.0

    Compiler Error Message: CS0117: ‘ASP.stockdetails_aspx’ does not contain a definition for ‘Button1_Click’

    Please help
    kind regards
    alok aggarwal

  46. Sir,

    Can you please tell me how to remove columns of gridview during printing??

    Thank you.

    1. Sorry for distrub…

      can you please tell me how to remove edit,delete columns of gridview while printing.

      The above code can print any content between div tag, it displays.

      can any one please help me……

      Thanks and Regards
      Rayudu

  47. Azeem Uddin · · Reply

    hi patriwala,
    above code works fine,i have 16columns when i give the print cammand in landscape mode its printing only 10 columns.can you give any idea how to print all 16 column in A4 size paper

    Thanks And Regards
    Mohammed Azeemuddin

  48. Deepak Singhal · · Reply

    Hi everyone
    actually i was required to print all the data of a grid when there is paging allowed and each page is containing 10 records i am successfully able to print all the records of gridview by using following code

    aspx.cs code:-
    —————-
    protected void btnPrint_Click(object sender, EventArgs e)
    {
    gvTemplate.AllowPaging = false;
    //gvTemplate.DataSource = dtprint;
    gvTemplate.DataSource = (Session[“prntdata”]);
    gvTemplate.DataBind();
    StringWriter sw = new StringWriter();
    HtmlTextWriter ht = new HtmlTextWriter(sw);
    PlaceHolder phTemp = new PlaceHolder();
    phTemp.Controls.Add(gvTemplate);
    phTemp.RenderControl(ht);
    StringBuilder sb = new StringBuilder();

    sb.Append(“var mywin = window.open(”,’printPage’,’letf=0,top=0,width=800%,height=600,scrollbars=1,resizable=1′);”);
    sb.Append(“mywin.document.open();”);
    sb.Append(“mywin.document.write(‘Print Page’);”);
    sb.Append(“mywin.document.write(”);”);
    sb.Append(“mywin.document.write(‘” + sw.ToString().Replace(Environment.NewLine, “”).Replace(“\t”, “”) + “‘);”);
    sb.Append(“mywin.document.write(”);”);
    sb.Append(“mywin.document.close();”);
    sb.Append(“mywin.focus();”);
    ClientScript.RegisterClientScriptBlock(this.GetType(), “Print”, sb.ToString(), true);

    phMain.Controls.Add(gvTemplate);
    gvTemplate.AllowPaging = true;
    //gvTemplate.DataSource = (Session[“prntdata”]);
    bindGrid();
    gvTemplate.DataBind();

    }

    aspx code:—–
    ————–

    <asp:Label ID="lblCName" runat="server" Text='’>

    <asp:Label ID="lblSName" runat="server" Text='’>

    <asp:Label ID="lblFName" runat="server" Text='’>

    <asp:Label ID="lblDate" runat="server" Text='’>

    <asp:Label ID="lblStatus" runat="server" Text='’>

    <asp:Label ID="lblReason" runat="server" Text='’>

  49. Naresh · · Reply

    Good Work Guys,
    Helped me a lot
    Post more articles
    Awesome Work

    1. can you please send your complet code with the aspx file to my email?? i need it

  50. HI!
    sir i work in asp.net and i get short program in asp.net.so plz help me .

  51. V.R.Hari · · Reply

    sorry… the style=font-size:10px, in the div tag containing the grid view , doesnt seem to work. the printed gridview still has large fonts. any other solutions please…

    thanx….

  52. I just read a few other posts on here. Glad I returned another time. Cudos

  53. sir can you please send me your complete code, i need it, cause i spend lots of time with your code but still having problem, and i need each page header.

    thanks’

  54. Manjeet · · Reply

    In Google Chrome 10.0 it is not working, can anybody provide solution.

  55. Patriwala u r the real man.
    thanks for your help buddy. its quite simple and anyone can understand it easily. keep it up 🙂

  56. its show “print preview faild” with Google Chrome
    anybody have solve ???

  57. gowri shankar · · Reply

    hi
    for vb also working
    here id =:innerdata” means look at belove

    tablecell id =’innerdata” then it will takes all things from that

    thanks for this post ..good work ..

  58. surendra · · Reply

    How to print table-er format using those java-script function. i want same grid print what grid containing

    can you any one having this Pls,
    send mail code v.suri87@gmail.com

  59. Hi Patriwala,

    I am using asp.net2.0 with vb. I dont want to show the datagrid(visible = false) and print all the records in that grid.
    Can u please send me the code. its very urgent

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s