Connecting Tech Pros Worldwide Help | Site Map

Printing Datagrid items in ASP.NET

Newbie
 
Join Date: Jan 2009
Posts: 15
#1: Mar 17 '09
Please provide me the code snippet for printing datagrid items.
i have a datagrid binded to a datatable.
On click of print button, i should print the datagrid as such using c#

Regards,
Blacky
BeemerBiker's Avatar
Member
 
Join Date: Jul 2008
Location: San Antonio, Texas
Posts: 68
#2: Mar 17 '09

re: Printing Datagrid items in ASP.NET


I just went thru this. AFAICT, programmatically executing "window.print()", or whatever, is no different than the user clicking on the browser and selecting "print". I am working with 4 gridviews. However, my app needs to print a number of "reports". I elected to display a printer friendly grid with autogen columns=true on a new page and let the use simply select the "print" command from the browser. He or she will have to set up a landscape printer beforehand and name that printer as "the report printer" or whatever.

FWIW, the following code launches my "printableable page"
Expand|Select|Wrap|Line Numbers
  1.             Session["ReportSelectCommand"] = SelectCommand;
  2.             string navigate = "<script>window.open('./printit.aspx');</script>";
  3.             Page.RegisterStartupScript("open", navigate); 
  4.  
When the "print" page starts up, it executes the query and at RowCreated I replace the field names with captions that make sense.

Expand|Select|Wrap|Line Numbers
  1.     protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
  2.         {
  3.             if (e.Row.RowIndex == -1 && e.Row.RowType == DataControlRowType.Header)
  4.             {
  5.                 FormatHdr(e.Row.Cells);
  6.             }
  7.         }
  8.  
When the header row is reformatted I basically lookup the field name in my symboltable and return with a caption, a nice "px" width and a string that represents the DataFormatString if one is required.

None of this would be necessary if I hand coded up gridviews for each report (ie: autogen column=false). However, the number of reports and layout is changing daily and the above is convenient.

HTH
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,751
#3: Mar 17 '09

re: Printing Datagrid items in ASP.NET


Quote:

Originally Posted by Blacky View Post

Please provide me the code snippet for printing datagrid items.
i have a datagrid binded to a datatable.
On click of print button, i should print the datagrid as such using c#

Regards,
Blacky

In the future, please give it a go on your own before asking someone to write your code for you. The folks here are very helpful and generous with donating their time to help people learn the ins and outs writing code.

If you show the code you have completed so far... explain where it is going wrong or where you have run into a block... provide the error messages etc. someone can help you get back on track.

Its like the old saying: "Give me a fish I eat for a day... Teach me to fish and I get drunk on my boat every weekend."
Newbie
 
Join Date: Jan 2009
Posts: 15
#4: Mar 18 '09

re: Printing Datagrid items in ASP.NET


actually i tried using window.print(),as javascript
Expand|Select|Wrap|Line Numbers
  1.  function PrintWindow()
  2.     {
  3.     document.getElementById('div_buttons').style.visibility = 'hidden';
  4.     window.print();    
  5.     }
Since my grid is in div tag, it prints the datagrid but with the scroll and moreover the lowermost part of the datagrid details are not printed.Only the details that is visible before scrolling are printed.

How to solve this....??
BeemerBiker's Avatar
Member
 
Join Date: Jul 2008
Location: San Antonio, Texas
Posts: 68
#5: Mar 18 '09

re: Printing Datagrid items in ASP.NET


I have the same problem. All my grids are in a div with a scrollbar as my boss wants the web page to behave like the windows form he is used to using. I tried to get him to accept the paging scheme but he wanted it scrolled. AFAICT there is no 3rd party scrolling grid for aspnet and the headers tend to scroll with the div.

Anyway, I (just today) discovered this method of printing a report and haveing a page break after x number of rows

http://msdn.microsoft.com/en-us/library/aa479018.aspx

I want to have "headers" printed at the top of each page and I think this possibly will do it.

There is another thread in this forum where a css is used to control whether the background prints or not. Some of these might be usefull
http://www.webdeveloper.com/forum/showthread.php?t=1406

I got the following in an email but have not figured out how it works yet
Expand|Select|Wrap|Line Numbers
  1. /* Make Classifications show up in b&w printouts */
  2. @media print {
  3.      #classification_div, #classification_dynamic div {
  4.          background-color: white !important;
  5.          color: black !important;
  6.      }
  7. }
  8.  
and a really good article about "nice" printing of pages
http://www.alistapart.com/articles/goingtoprint/
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,081
#6: Mar 18 '09

re: Printing Datagrid items in ASP.NET


Quote:

Originally Posted by Blacky View Post

actually i tried using window.print(),as javascript

Expand|Select|Wrap|Line Numbers
  1.  function PrintWindow()
  2.     {
  3.     document.getElementById('div_buttons').style.visibility = 'hidden';
  4.     window.print();    
  5.     }
Since my grid is in div tag, it prints the datagrid but with the scroll and moreover the lowermost part of the datagrid details are not printed.Only the details that is visible before scrolling are printed.

How to solve this....??

This seems to be a pretty straight forward problem.

What you want to do is create a style sheet that will be applied to your page when the user is Printing.

It's just like a normal style sheet but is only applied to the page when the page is printing.

You'd add it to your page exactly as you would add a normal style sheet to the page, except that you would change the media to "print". For example:
Expand|Select|Wrap|Line Numbers
  1. <link type="text/css" rel="stylesheet" media="print" href="~/PrintStyle.css"></link>
In this style sheet you would create a class with "overflow:none" that you can apply to your div. When you do this the scroll bars will not be printed.

For example, you may have the CSS class defined in your page's style sheet, which is used by the <div> that contains your grid, so that scroll bars will be displayed:
Expand|Select|Wrap|Line Numbers
  1. .mainDivStyle{
  2.     overflow:auto;
  3.     height: 150px;
  4.     width: 250px;
  5. }
In order to remove the scroll bars when the page is being printed you would add the same class to your print style sheet but change the overflow style:
Expand|Select|Wrap|Line Numbers
  1. .mainDivStyle{
  2.     overflow:none;
  3.     height: 150px;
  4.     width: 250px;
  5. }

Now when the page is being viewed in the browser the <div> will have scroll bars. But when the page is being printed, there will be no scroll bars.


For more information please see w3c.

-Frinny
Reply


Similar ASP.NET bytes