How To Make Printer Friendly Page?

Hi,
My Question iz how can we make a printer friendly page with the datagrid populated. any ideas?
Thanks in AdvanceBig Smile
[192 byte] By [QadeerAhmed] at [2007-12-17]
# 1
Hi,

you may user a 3rd party reporting tool, or do it your self.

If you decide to do it your self take a look here:

http://www.developerfusion.co.uk/show/4585/

rgerbig at 2007-9-8 > top of Msdn Tech,Visual Studio Express Editions,Installing and Registering Visual Studio 2005 Express Editions...
# 2
MSDN Library offers the following guidance...

This example demonstrates printing a DataGrid control.

Example

private void printGrid_Click(System.Object sender, System.EventArgs e) { printDocument1.Print(); } private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e) { PaintEventArgs myPaintArgs = new PaintEventArgs(e.Graphics, new Rectangle(new Point(0, 0), this.Size)); this.InvokePaint(dataGrid1, myPaintArgs); }

However, when I tried it in VS2005, it did appeared to be broken.
I think because VS2005 uses GDI+ rather than GDI.
So I tried the new DataGridView doing something similar, but it appears to
be broken also.
Hopefully, you are still using VS2003, or can wait till Beta 3.

GraemeWT at 2007-9-8 > top of Msdn Tech,Visual Studio Express Editions,Installing and Registering Visual Studio 2005 Express Editions...
# 3
All versions of .NET have used GDI+. The only way to use GDI from .NET is to P/Invoke it.
someotherguy at 2007-9-8 > top of Msdn Tech,Visual Studio Express Editions,Installing and Registering Visual Studio 2005 Express Editions...
# 4
SadI got the errors the following error while compiling:

C:\Inetpub\wwwroot\Project\process.aspx.cs(386): The type or namespace name 'PaintEventArgs' could not be found (are you missing a using directive or an assembly reference?)

C:\Inetpub\wwwroot\Project\process.aspx.cs(383): The type or namespace name 'printDocument1' could not be found (are you missing a using directive or an assembly reference?)

C:\Inetpub\wwwroot\Project\process.aspx.cs(386): The name 'myPaintArgs' does not exist in the class or namespace 'Project.Process'

C:\Inetpub\wwwroot\Project\process.aspx.cs(386): 'Project.Process' does not contain a definition for 'InvokePaint'

QadeerAhmed at 2007-9-8 > top of Msdn Tech,Visual Studio Express Editions,Installing and Registering Visual Studio 2005 Express Editions...
# 5
My comment about GDI and GDI+ was based on reading the following post from the DataGridView Program Manager
I am still searching for a practical way to print a DataGRidView.

Wow, I haven't see that, unless you are using TextRenderer.DrawText for your printing. Unfortunitely TextRenderer.DrawText uses GDI while Graphics.DrawString uses GDI+. GDI drawing with our current print engine doesn't work correctly, so you'll need to use Graphics.DrawString.

Let me know if that works. If that isn't the case then can you file a bug and we'll investigate it? Thanks

-mark
DataGridView Program Manager
Microsoft

GraemeWT at 2007-9-8 > top of Msdn Tech,Visual Studio Express Editions,Installing and Registering Visual Studio 2005 Express Editions...
# 6
Here is a test class that attemps to print a DataGridView. It executes but the printed document contains only part of the DataGridView! Hope it helps.

The method is invoked from a MainForm class that contains a DataGridView publicly exposed as:
public DataGridView DataGridView { get { return dataGridView1; } }

The code to invoke the printing is:
private void printToolStripMenuItem_Click(object sender, EventArgs e)
{Print.
Timesheet timesheetPrint = new Timex.Print.Timesheet(this);}
public void DoInvokePaint(Control paintObject, PaintEventArgs paintArgs)
{
this.InvokePaint(paintObject, paintArgs);}

The Print.Timesheet class is:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.Text;

namespace Timex.Print

{

class Timesheet

{

public Timesheet(MainForm form)

{

mainForm = form;

_PrintDocument = new System.Drawing.Printing.PrintDocument();

_PrintDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(_PrintDocument_PrintPage);

SetPrintProperties(form);

}

private Font printFont;

private System.Drawing.Printing.PrintDocument _PrintDocument;

public System.Drawing.Printing.PrintDocument PrintDocument { get { return _PrintDocument; } }

private System.Windows.Forms.PrintDialog _PrintDialog;

public System.Windows.Forms.PrintDialog PrintDialog { get { return _PrintDialog; } }

private MainForm mainForm;

// This method will set properties on the PrintDialog object and then display the dialog.

private void SetPrintProperties(System.Windows.Forms.Form form)

{

_PrintDialog = new System.Windows.Forms.PrintDialog();

// Allow the user to choose the page range he or she would like to print.

_PrintDialog.AllowSomePages = true;

// Show the help button.

_PrintDialog.ShowHelp = true;

// Set the Document property to the PrintDocument for

// which the PrintPage Event has been handled. To display the

// dialog, either this property or the PrinterSettings property

// must be set

_PrintDialog.Document = this._PrintDocument;

DialogResult result = _PrintDialog.ShowDialog();

printFont = new Font("Arial", 10, FontStyle.Regular);

// If the result is OK then print the document.

if (result == DialogResult.OK)

{

_PrintDocument.Print();

}

}

/// <summary>

/// The PrintPage event is raised for each page to be printed.

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void _PrintDocument_PrintPage(object sender, PrintPageEventArgs e)

{

// Insert code to render the page here.

// This code will be called when the control is drawn.

// The following code will render a simple message on the printed document.

string text = "In document_PrintPage method.";

// Draw the content.

e.Graphics.DrawString(text, printFont, System.Drawing.Brushes.Black, 10, 10);

PaintEventArgs paintArgs = new PaintEventArgs(e.Graphics, new Rectangle(new Point(50, 50), new Size(400,400)));

mainForm.DoInvokePaint(mainForm.DataGridView, paintArgs);

}

}

}

GraemeWT at 2007-9-8 > top of Msdn Tech,Visual Studio Express Editions,Installing and Registering Visual Studio 2005 Express Editions...
# 7

Try using System.Windows.Forms.Control.DrawToBitmap method: this method prints the data grid view to a bitmap.

Hope this helps.

DanielHerlingMicrosoft at 2007-9-8 > top of Msdn Tech,Visual Studio Express Editions,Installing and Registering Visual Studio 2005 Express Editions...