如何将System.Windows.Forms.DataGrid打印到纸张上? 我在C#中使用.NET 3.5框架
答案 0 :(得分:4)
以下是使用System.Drawing.Printing.PrintDocument
类的示例(不完整,但展示了如何将所有内容连接在一起):
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);
}
}