How can I print a System.Windows.Forms.DataGrid to paper? I'm usin开发者_高级运维g .NET 3.5 framwork in C#
Here is an example using the System.Drawing.Printing.PrintDocument
class (not complete, but shows how to wire everything together):
public class MyForm : Form
{
DataGrid dataGrid1 = new DataGrid();
Button printGrid = new Button();
PrintDocument printDocument1 = new PrintDocument();
public MyForm()
{
printGrid.Click += new EventHandler(printGrid_Click);
printDocument1.PrintPage +=
new PrintPageEventHandler(printDocument1_PrintPage);
}
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);
}
}
精彩评论