From: JohnE on
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


From: Mark Rae [MVP] on
"JohnE" <JohnE(a)discussions.microsoft.com> wrote in message
news:3EC3510B-B241-4B17-847B-9F1B813BECB9(a)microsoft.com...

> 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

From: Andy O'Neill on

"JohnE" <JohnE(a)discussions.microsoft.com> wrote in message
news:3EC3510B-B241-4B17-847B-9F1B813BECB9(a)microsoft.com...
> 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.

From: JohnE on


"Mark Rae [MVP]" wrote:

> "JohnE" <JohnE(a)discussions.microsoft.com> wrote in message
> news:3EC3510B-B241-4B17-847B-9F1B813BECB9(a)microsoft.com...
>
> > 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?"
From: JohnE on


"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.