开发者

How can I print Winforms DataGrid to paper?

开发者 https://www.devze.com 2023-01-17 05:06 出处:网络
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 s

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);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消