"JohnE" <> wrote in message
news:3EC3510B-B241-4B17-847B-...
> 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