printing a gridview's contents

Discussion in 'ASP .Net' started by JohnE, Mar 29, 2010.

  1. JohnE

    JohnE Guest

    Hello. I am having a heckuva time with the printing of a gridview's
    contents, all not just one page. And the more I google, the more confused I
    get. What is needed is to take the current gridview with paging, sorting,
    etc. There is one column to omit which is the allow edits. Since there is
    paging, I need to have that removed as well so the page numbers at the bottom
    of the grid do not show. Column headers can stay but without the sorting
    lines. No hyperlinks at all. Background coloring, row coloring, and the
    like, can be optional. For some reason I am not getting any way I try it to
    work, whether it be with a printer friendly or straight to the printer.

    Does anyone know of a sure fire method, sample, example, website that can
    help out with this?

    Thanks in advance for any help/assistance on this.
    .... John
     
    JohnE, Mar 29, 2010
    #1
    1. Advertising

  2. "JohnE" <> wrote in message
    news:...

    > Does anyone know of a sure fire method, sample, example, website that can
    > help out with this?


    Firstly, take a step back here... A GridView is nothing more than one method
    of presenting data to the user, specifically to the screen. What you're
    looking for now is a different method of presenting data to the user, this
    time specifically to the printer.

    So, forget completely any notion of "printing the GridView". Instead, you
    need to be thinking about printing the underlying data. This is why you're
    going round in circles.

    So, how are you getting the underlying data before binding it to the
    GridView? I'm hoping (fingers crossed!) that you're using a DAL or, at the
    very least, using code-behind to fetch the data into either a DataSet /
    DataTable or DataReader object which you are then binding to the GridView.
    (If you're using one of the SqlDataSource "training wheels" objects, then
    what follows might not work...)

    Secondly, you need to decide how you are going to get the data to the
    printer itself. You're posting in an ASP.NET forum, so I'm assuming that
    this a browser-based solution. If so, FORGET COMPLETELY any notion of trying
    to print directly, especially if the web application is hosted on the public
    Internet. All modern browsers are configured to prevent this sort of direct
    interfacing with the hardware and software of the machine on which they are
    running. This is also why so many public web apps have a "print" facility
    which does nothing of the sort. Instead, it creates a simplified view of the
    data to be printed, usually in a separate window or as a PDF, which the user
    will then print manually.

    My personal preference is to use PDF for web printing, as this avoids any
    cross-browser inconsistencies and also allows the user to save a copy if
    required. To create the PDFs, I use this: http://www.siberix.com/

    So, let's say your page looks vaguely like this:

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    BindData();
    }
    }

    private void BindData()
    {
    DataSet MyDS = FetchData(); // fetch data from DAL
    MyDS.Tables[0].DefaultView.Sort = ViewState["Sort"].ToString();
    MyGridView.DataSource = MyDS.Tables[0].DefaultView;
    }

    I would then add a Print button which fetches the same data, renders it as a
    PDF, and then presents it on the screen so that the user can print it
    manually, if required e.g.

    protected void cmdPrint_Click(object sender, EventArgs e)
    {
    DataSet MyDS = FetchData(); // fetch data from DAL
    MyDS.Tables[0].DefaultView.Sort = ViewState["Sort"].ToString();

    // create the PDF
    // document info
    // document header
    // data
    foreach (DataRowView MyRow in MyDS.Tables[0].DefaultView)
    {
    // render the individual row(s) to the PDF
    }
    // document footer

    Response.Clear();
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;
    filename=Journal.pdf");
    // stream the PDF to the screen
    Response.End();
    }


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net
     
    Mark Rae [MVP], Mar 29, 2010
    #2
    1. Advertising

  3. JohnE

    Andy O'Neill Guest

    "JohnE" <> wrote in message
    news:...
    > Hello. I am having a heckuva time with the printing of a gridview's
    > contents, all not just one page.


    As Mark has said, you want some sort of reporting tool rather than trying to
    print directly.
    I've used SSRS and crystal reporting extensively on intranet systems.
    I've also used xslt to give the user an excel version of the data.
    Users are often inordinately keen on a solution which involves excel.

    If extracting the data is resource intensive then you could consider caching
    the data each time the gridview is extracted.
    Otherwise, make your data extract standard and re-read for the report.
     
    Andy O'Neill, Mar 29, 2010
    #3
  4. JohnE

    JohnE Guest

    "Mark Rae [MVP]" wrote:

    > "JohnE" <> wrote in message
    > news:...
    >
    > > Does anyone know of a sure fire method, sample, example, website that can
    > > help out with this?

    >
    > Firstly, take a step back here... A GridView is nothing more than one method
    > of presenting data to the user, specifically to the screen. What you're
    > looking for now is a different method of presenting data to the user, this
    > time specifically to the printer.
    >
    > So, forget completely any notion of "printing the GridView". Instead, you
    > need to be thinking about printing the underlying data. This is why you're
    > going round in circles.
    >
    > So, how are you getting the underlying data before binding it to the
    > GridView? I'm hoping (fingers crossed!) that you're using a DAL or, at the
    > very least, using code-behind to fetch the data into either a DataSet /
    > DataTable or DataReader object which you are then binding to the GridView.
    > (If you're using one of the SqlDataSource "training wheels" objects, then
    > what follows might not work...)
    >
    > Secondly, you need to decide how you are going to get the data to the
    > printer itself. You're posting in an ASP.NET forum, so I'm assuming that
    > this a browser-based solution. If so, FORGET COMPLETELY any notion of trying
    > to print directly, especially if the web application is hosted on the public
    > Internet. All modern browsers are configured to prevent this sort of direct
    > interfacing with the hardware and software of the machine on which they are
    > running. This is also why so many public web apps have a "print" facility
    > which does nothing of the sort. Instead, it creates a simplified view of the
    > data to be printed, usually in a separate window or as a PDF, which the user
    > will then print manually.
    >
    > My personal preference is to use PDF for web printing, as this avoids any
    > cross-browser inconsistencies and also allows the user to save a copy if
    > required. To create the PDFs, I use this: http://www.siberix.com/
    >
    > So, let's say your page looks vaguely like this:
    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > if (!IsPostBack)
    > {
    > BindData();
    > }
    > }
    >
    > private void BindData()
    > {
    > DataSet MyDS = FetchData(); // fetch data from DAL
    > MyDS.Tables[0].DefaultView.Sort = ViewState["Sort"].ToString();
    > MyGridView.DataSource = MyDS.Tables[0].DefaultView;
    > }
    >
    > I would then add a Print button which fetches the same data, renders it as a
    > PDF, and then presents it on the screen so that the user can print it
    > manually, if required e.g.
    >
    > protected void cmdPrint_Click(object sender, EventArgs e)
    > {
    > DataSet MyDS = FetchData(); // fetch data from DAL
    > MyDS.Tables[0].DefaultView.Sort = ViewState["Sort"].ToString();
    >
    > // create the PDF
    > // document info
    > // document header
    > // data
    > foreach (DataRowView MyRow in MyDS.Tables[0].DefaultView)
    > {
    > // render the individual row(s) to the PDF
    > }
    > // document footer
    >
    > Response.Clear();
    > Response.ContentType = "application/pdf";
    > Response.AddHeader("content-disposition", "attachment;
    > filename=Journal.pdf");
    > // stream the PDF to the screen
    > Response.End();
    > }
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
    >


    Yup, it's the "training wheels" SqlDataSource object that is getting the
    data to the gridview. And, yup, what you provided does not work. But what
    you did send can be kept for when DAL is used on the next webapp. Any
    thoughts on how to proceed with the "training wheels?"
     
    JohnE, Mar 29, 2010
    #4
  5. JohnE

    JohnE Guest

    "JohnE" wrote:

    > Hello. I am having a heckuva time with the printing of a gridview's
    > contents, all not just one page. And the more I google, the more confused I
    > get. What is needed is to take the current gridview with paging, sorting,
    > etc. There is one column to omit which is the allow edits. Since there is
    > paging, I need to have that removed as well so the page numbers at the bottom
    > of the grid do not show. Column headers can stay but without the sorting
    > lines. No hyperlinks at all. Background coloring, row coloring, and the
    > like, can be optional. For some reason I am not getting any way I try it to
    > work, whether it be with a printer friendly or straight to the printer.
    >
    > Does anyone know of a sure fire method, sample, example, website that can
    > help out with this?
    >
    > Thanks in advance for any help/assistance on this.
    > ... John
    >
    >


    Maybe I am looking at this wrong. If a user (all are internal to company)
    wants to print the gridview (actually the datasource) it would be considered
    more of a report than anything else. I should look at using the reportviewer
    or sending the user to a SSRS report like Mr O'Neill mentioned. We already
    use SSRS as our reporting mechanism.
     
    JohnE, Mar 29, 2010
    #5
  6. "JohnE" <> wrote in message
    news:...

    > Any thoughts on how to proceed with the "training wheels?"


    No, sorry... :-(


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net
     
    Mark Rae [MVP], Mar 29, 2010
    #6
  7. On Mar 29, 1:35 am, JohnE <> wrote:
    > Hello.  I am having a heckuva time with the printing of a gridview's
    > contents, all not just one page.  And the more I google, the more confused I
    > get.  What is needed is to take the current gridview with paging, sorting,
    > etc.   There is one column to omit which is the allow edits.  Since there is
    > paging, I need to have that removed as well so the page numbers at the bottom
    > of the grid do not show.  Column headers can stay but without the sorting
    > lines.  No hyperlinks at all.  Background coloring, row coloring, and the
    > like, can be optional.  For some reason I am not getting any way I try it to
    > work, whether it be with a printer friendly or straight to the printer.
    >
    > Does anyone know of a sure fire method, sample, example, website that can
    > help out with this?  
    >
    > Thanks in advance for any help/assistance on this.
    > ... John


    Don't make it complicated. From what I get is that you want the same
    grid, but without paging. Just make a copy of your webform, disable
    paging and sorting and get rid of editable columns. That's all. Just
    link to the new form by adding a [Print] link and you're done.
     
    Alexey Smirnov, Mar 29, 2010
    #7
  8. JohnE

    JohnE Guest

    "Mark Rae [MVP]" wrote:

    > "JohnE" <> wrote in message
    > news:...
    >
    > > Any thoughts on how to proceed with the "training wheels?"

    >
    > No, sorry... :-(
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
    >


    That's alright. At least you responded. As I mentioned in another reply,
    might look at this thru a report mechanism. Then I can add formatting, etc
    that the users will eventually ask for. Much simpler solution.
    Thanks.
     
    JohnE, Mar 29, 2010
    #8
  9. JohnE

    JohnE Guest

    "Alexey Smirnov" wrote:

    > On Mar 29, 1:35 am, JohnE <> wrote:
    > > Hello. I am having a heckuva time with the printing of a gridview's
    > > contents, all not just one page. And the more I google, the more confused I
    > > get. What is needed is to take the current gridview with paging, sorting,
    > > etc. There is one column to omit which is the allow edits. Since there is
    > > paging, I need to have that removed as well so the page numbers at the bottom
    > > of the grid do not show. Column headers can stay but without the sorting
    > > lines. No hyperlinks at all. Background coloring, row coloring, and the
    > > like, can be optional. For some reason I am not getting any way I try it to
    > > work, whether it be with a printer friendly or straight to the printer.
    > >
    > > Does anyone know of a sure fire method, sample, example, website that can
    > > help out with this?
    > >
    > > Thanks in advance for any help/assistance on this.
    > > ... John

    >
    > Don't make it complicated. From what I get is that you want the same
    > grid, but without paging. Just make a copy of your webform, disable
    > paging and sorting and get rid of editable columns. That's all. Just
    > link to the new form by adding a [Print] link and you're done.
    > .
    >


    Alexey, thanks for the "simple" way. And it was simple. I showed to
    several of the users and they both said it is what they wanted. Although, I
    am having difficulty in getting the PRINT button to work correctly. Below is
    the line that I have in the btnPrint_Click (asp button) event. It opens up
    the form with exactly what is needed. But I am not able to get the print
    dialog to open. I've tried several ways but to no avail.

    ClientScript.RegisterStartupScript(this.GetType(), "onclick",
    "<script
    language=javascript>window.open('ProteusListPrint.aspx','','height=500px,width=800px,scrollbars=1');</script>");

    What is missing to open the print dialog box? Also, in the print out in the
    bottom left corner is the http address. Is there any 'simple' way to
    hide/remove it?

    Thanks...John
     
    JohnE, Mar 30, 2010
    #9
  10. JohnE

    JohnE Guest

    No response needed. I figured it out.
    Thanks.


    "JohnE" wrote:

    >
    >
    > "Alexey Smirnov" wrote:
    >
    > > On Mar 29, 1:35 am, JohnE <> wrote:
    > > > Hello. I am having a heckuva time with the printing of a gridview's
    > > > contents, all not just one page. And the more I google, the more confused I
    > > > get. What is needed is to take the current gridview with paging, sorting,
    > > > etc. There is one column to omit which is the allow edits. Since there is
    > > > paging, I need to have that removed as well so the page numbers at the bottom
    > > > of the grid do not show. Column headers can stay but without the sorting
    > > > lines. No hyperlinks at all. Background coloring, row coloring, and the
    > > > like, can be optional. For some reason I am not getting any way I try it to
    > > > work, whether it be with a printer friendly or straight to the printer.
    > > >
    > > > Does anyone know of a sure fire method, sample, example, website that can
    > > > help out with this?
    > > >
    > > > Thanks in advance for any help/assistance on this.
    > > > ... John

    > >
    > > Don't make it complicated. From what I get is that you want the same
    > > grid, but without paging. Just make a copy of your webform, disable
    > > paging and sorting and get rid of editable columns. That's all. Just
    > > link to the new form by adding a [Print] link and you're done.
    > > .
    > >

    >
    > Alexey, thanks for the "simple" way. And it was simple. I showed to
    > several of the users and they both said it is what they wanted. Although, I
    > am having difficulty in getting the PRINT button to work correctly. Below is
    > the line that I have in the btnPrint_Click (asp button) event. It opens up
    > the form with exactly what is needed. But I am not able to get the print
    > dialog to open. I've tried several ways but to no avail.
    >
    > ClientScript.RegisterStartupScript(this.GetType(), "onclick",
    > "<script
    > language=javascript>window.open('ProteusListPrint.aspx','','height=500px,width=800px,scrollbars=1');</script>");
    >
    > What is missing to open the print dialog box? Also, in the print out in the
    > bottom left corner is the http address. Is there any 'simple' way to
    > hide/remove it?
    >
    > Thanks...John
     
    JohnE, Mar 30, 2010
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aaron
    Replies:
    0
    Views:
    394
    Aaron
    Dec 29, 2005
  2. Don Adams
    Replies:
    1
    Views:
    624
    Martin Honnen
    Mar 5, 2004
  3. Kamarulnizam Rahim
    Replies:
    4
    Views:
    246
    Robert Klemme
    Jan 28, 2011
  4. Amit
    Replies:
    0
    Views:
    220
  5. Replies:
    0
    Views:
    217
Loading...

Share This Page