473,387 Members | 3,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Printing Datagrid items in ASP.NET

40
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
Mar 17 '09 #1
5 3502
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
Mar 17 '09 #2
tlhintoq
3,525 Expert 2GB
@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."
Mar 17 '09 #3
Blacky
40
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....??
Mar 18 '09 #4
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/
Mar 18 '09 #5
Frinavale
9,735 Expert Mod 8TB
@Blacky
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
Mar 18 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: NickB | last post by:
Please could someone tell me what is wrong. Ther error is: An unhandled exception of type 'System.NullReferenceException' occurred in microsoft.visualbasic.dll Additional information: Object...
2
by: pei_world | last post by:
I want to implement a key hit with enter to dropdown a combobox that is in the datagrid. in this case I need to override its original behaviours. I found some codes from the web. Does anyone know...
4
by: Glenn Owens | last post by:
I have a DataGrid web control which I've dynamically populated with template columns to be used for bulk-editting. Generally, all of the columns are textbox and/or dropdownlist child controls. ...
2
by: Allen Davis | last post by:
I have some hierarchical data bound to a series of nested DataLists and DataGrids for which I'd like to be able to provide the end-user some targeted printing capabilities. By that I mean being...
0
by: Danny | last post by:
I am trying to sort my second datagrid. And it works but only after i click 2 times on the column header. The first time i click on the header the data in the right order will add to the rows in...
9
by: tshad | last post by:
How do I find (and set) a couple of labels in the Footer after a DataGrid is filled? I have a bunch of DataGrids that get displayed nested inside a DataList. The datagrid looks like: ...
4
by: Jay | last post by:
Still can't seem to find a solution to printing a lengthy datagrid on multiple pages including datagrid headeron each page. I am not using Crystal Reports or Reporting Services or VStudio. Any...
0
by: Wes Ong via .NET 247 | last post by:
Hi, I am trying to print a webform (ASP.NET C#) that I have designed on a dot matrix printer. It is a delivery order hence the top will contain the name, information of the company etc... I am also...
5
by: ComputerStop | last post by:
I am attempting to print a datagridview. I have not found any method that works successfully. Has any one been successful with this?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.