printing a gridview's contents

  • Thread starter Thread starter JohnE
  • Start date Start date
J

JohnE

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
 
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();
}
 
JohnE said:
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.
 
Mark Rae said:
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();
}

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 said:
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.
 
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, andthe
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.
 
Mark Rae said:
No, sorry... :-(

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.
 
Alexey Smirnov said:
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
 
No response needed. I figured it out.
Thanks.


JohnE said:
Alexey Smirnov said:
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
 
Back
Top