Printing Datagrid items in ASP.NET

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Blacky
    New Member
    • Jan 2009
    • 40

    Printing Datagrid items in ASP.NET

    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
    New Member
    • Jul 2008
    • 87

    #2
    I just went thru this. AFAICT, programmaticall y 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 "printablea ble page"
    Code:
                Session["ReportSelectCommand"] = SelectCommand;
                string navigate = "<script>window.open('./printit.aspx');</script>";
                Page.RegisterStartupScript("open", navigate);
    When the "print" page starts up, it executes the query and at RowCreated I replace the field names with captions that make sense.

    Code:
        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowIndex == -1 && e.Row.RowType == DataControlRowType.Header)
                {
                    FormatHdr(e.Row.Cells);
                }
            }
    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 DataFormatStrin g 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

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      1) Research. 2) Attempt. 3) try again. 4) HELP!

      Originally posted by Blacky
      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."

      Comment

      • Blacky
        New Member
        • Jan 2009
        • 40

        #4
        actually i tried using window.print(), as javascript
        Code:
         function PrintWindow()
            {
            document.getElementById('div_buttons').style.visibility = 'hidden';
            window.print();    
            }
        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....??
        Last edited by Frinavale; Mar 18 '09, 07:44 PM. Reason: Added [code] tags. Please post code in [code] [/code] tags.

        Comment

        • BeemerBiker
          New Member
          • Jul 2008
          • 87

          #5
          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



          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


          I got the following in an email but have not figured out how it works yet
          Code:
          /* Make Classifications show up in b&w printouts */
          @media print {
               #classification_div, #classification_dynamic div {
                   background-color: white !important;
                   color: black !important;
               }
          }
          and a really good article about "nice" printing of pages
          Say no to “printer-friendly” versions and yes to printer-specific style sheets. CSS expert Eric Meyer shows how to conceive and design print style sheets that automatically format web c…

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Originally posted by Blacky
            actually i tried using window.print(), as javascript
            Code:
             function PrintWindow()
                {
                document.getElementById('div_buttons').style.visibility = 'hidden';
                window.print();    
                }
            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:
            Code:
            <link type="text/css" rel="stylesheet" media="print" href="~/PrintStyle.css"></link>
            In this style sheet you would create a class with "overflow:n one" 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:
            Code:
            .mainDivStyle{
                overflow:auto;
                height: 150px;
                width: 250px;
            }
            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:
            Code:
            .mainDivStyle{
                overflow:none;
                height: 150px;
                width: 250px;
            }

            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

            Comment

            Working...