DotNet Friends

July 26, 2008

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

50 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

    Comment by Abhiram — August 7, 2008 @ 4:41 pm

    • 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,,

      Comment by jyothi — June 15, 2009 @ 4:43 am

  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.

    Comment by patriwala — August 8, 2008 @ 5:42 am

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

    Comment by Mohamed — September 6, 2008 @ 2:37 am

  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

    Comment by patriwala — September 6, 2008 @ 4:47 am

  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….

    Comment by Mohamed — September 6, 2008 @ 3:43 pm

  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

    Comment by Mohamed — September 6, 2008 @ 3:49 pm

  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.

    Comment by patriwala — September 6, 2008 @ 4:34 pm

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

    Comment by Mohamed — September 6, 2008 @ 10:05 pm

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

    I dont understand :$

    it gives error ?

    can anyone write the code ?

    Comment by tcatu — October 6, 2008 @ 12:50 pm

  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.

    Comment by patriwala — October 6, 2008 @ 1:34 pm

  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

    Comment by Priya — December 4, 2008 @ 5:13 am

  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

    Comment by patriwala — December 4, 2008 @ 7:39 am

  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.

    Comment by Priya — December 5, 2008 @ 6:04 am

    • 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

      Comment by patriwala — December 5, 2008 @ 7:21 am

  14. Thanks you Patriwala,

    I changed with and height =1px.

    Very nice.

    again thanks.
    Regards Priya.

    Comment by Priya — December 5, 2008 @ 7:46 am

  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

    Comment by parag — December 12, 2008 @ 10:33 pm

    • hi parag,
      it print current page record.

      thnx

      Comment by patriwala — December 15, 2008 @ 6:06 am

  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

    Comment by vinit — December 18, 2008 @ 6:10 am

    • 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

      Comment by patriwala — December 18, 2008 @ 10:12 am

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

    Comment by vinit — December 18, 2008 @ 7:08 am

    • 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

      Comment by patriwala — December 18, 2008 @ 10:25 am

  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.

    Comment by Himaja — December 29, 2008 @ 9:29 am

    • 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

      Comment by patriwala — December 29, 2008 @ 1:39 pm

  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

    Comment by Himaja — December 30, 2008 @ 9:52 am

  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 …

    Comment by Mahesh — December 31, 2008 @ 11:42 am

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

    Comment by Williams — January 5, 2009 @ 9:54 am

    • 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

      Comment by patriwala — January 5, 2009 @ 12:23 pm

  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;

    Comment by Williams — January 13, 2009 @ 10:48 am

  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

    Comment by khushi — January 21, 2009 @ 5:44 am

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

    Comment by sameer — January 23, 2009 @ 11:12 pm

  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.

    Comment by karthik — January 26, 2009 @ 7:56 pm

  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;

    }

    Comment by neha — January 30, 2009 @ 8:49 am

  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

    Comment by KT — February 23, 2009 @ 10:41 am

  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

    Comment by Gul — May 3, 2009 @ 5:06 am

  29. 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…

    Comment by Suresh A — May 6, 2009 @ 7:02 am

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

    Pingback by Print in ASP.NET 2.0 « Sandy’s Blog – ASP.NET — May 19, 2009 @ 3:44 pm

  31. SHOWIN LOVE,

    Comment by Cvarovsky — July 6, 2009 @ 11:30 am

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

    Comment by Caletri — July 6, 2009 @ 11:41 am

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

    Comment by Kerstenbaum — July 6, 2009 @ 11:41 am

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

    Comment by Brutchak — July 6, 2009 @ 11:41 am

  35. :) ,

    Comment by Kalmupp — July 6, 2009 @ 11:42 am

  36. 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..

    Comment by Harry — August 19, 2009 @ 7:53 am

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

    Comment by kathe — September 29, 2009 @ 8:27 pm

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

    Comment by kathe — September 30, 2009 @ 9:08 am

  39. Nice work, thank you. . . .

    Comment by Pablo Simons — October 30, 2009 @ 7:12 pm

  40. how to repeat gridview header on new page

    Comment by Ravi — November 17, 2009 @ 2:32 am

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

    Comment by Ravi — November 17, 2009 @ 2:34 am

  42. 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.

    Comment by Alok Tripathi — November 21, 2009 @ 7:11 am


RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.