Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > printing a gridview's contents

Reply
Thread Tools

printing a gridview's contents

 
 
JohnE
Guest
Posts: n/a
 
      03-28-2010
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


 
Reply With Quote
 
 
 
 
Mark Rae [MVP]
Guest
Posts: n/a
 
      03-29-2010
"JohnE" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> 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

 
Reply With Quote
 
 
 
 
Andy O'Neill
Guest
Posts: n/a
 
      03-29-2010

"JohnE" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.

 
Reply With Quote
 
JohnE
Guest
Posts: n/a
 
      03-29-2010


"Mark Rae [MVP]" wrote:

> "JohnE" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
> > 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?"
 
Reply With Quote
 
JohnE
Guest
Posts: n/a
 
      03-29-2010


"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.
 
Reply With Quote
 
Mark Rae [MVP]
Guest
Posts: n/a
 
      03-29-2010
"JohnE" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

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


No, sorry...


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

 
Reply With Quote
 
Alexey Smirnov
Guest
Posts: n/a
 
      03-29-2010
On Mar 29, 1:35*am, JohnE <(E-Mail Removed)> 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.
 
Reply With Quote
 
JohnE
Guest
Posts: n/a
 
      03-29-2010


"Mark Rae [MVP]" wrote:

> "JohnE" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
> > 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.
 
Reply With Quote
 
JohnE
Guest
Posts: n/a
 
      03-30-2010


"Alexey Smirnov" wrote:

> On Mar 29, 1:35 am, JohnE <(E-Mail Removed)> 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
 
Reply With Quote
 
JohnE
Guest
Posts: n/a
 
      03-30-2010
No response needed. I figured it out.
Thanks.


"JohnE" wrote:

>
>
> "Alexey Smirnov" wrote:
>
> > On Mar 29, 1:35 am, JohnE <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:52 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:50 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:28 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-18-2007 10:11 AM
Printing IFrame is printing whole window contents Amit Javascript 0 09-08-2006 02:19 AM



Advertisments