如何在多个页面上打印大型dataGridView / Form?

时间:2012-02-28 20:27:42

标签: c# winforms

在C#中使用Win Forms。创建数据网格视图并添加打印按钮。这是我的打印按钮代码。

private void toolStripButton1_Click(object sender, EventArgs e)
    {
        image = new Bitmap(this.dataGridView1.Width, this.dataGridView1.Height);
        this.dataGridView1.DrawToBitmap(image, new Rectangle(new Point(), this.dataGridView1.Size));
        memStream = new System.IO.MemoryStream();
        printPDialog = new PrintPreviewDialog();

        try
        {
            printFont = new Font("Arial", 10);
            image.Save(memStream, System.Drawing.Imaging.ImageFormat.Bmp);
            image.Save("d:\\Adnan.bmp");
            p = new PrintDocument();
            p.PrintPage += this.p_PrintPage;

            this.printPDialog.Document = p;
            printPDialog.Show();

        }
        catch (System.Runtime.InteropServices.ExternalException ee)
        {

        }

}

    private void p_PrintPage(object sender, PrintPageEventArgs e)
    {
        float linesPerPage = 0;
        float yPos = 0;
        int count = 0;
        float leftMargin = e.MarginBounds.Left;
        float topMargin = e.MarginBounds.Top;
        int? line = null;

        // Calculate the number of lines per page.
        linesPerPage = e.MarginBounds.Height /
           printFont.GetHeight(e.Graphics);   
        e.Graphics.DrawImage(Image.FromStream(memStream), leftMargin, topMargin);

    }


    private void printPDialog_FormClosed(object sender, FormClosedEventArgs e)
    {
        image.Dispose();
        memStream.Dispose();
    }

问题是我的DataGridView有一个很大的列表。我在FlowLayoutPanel中有DataGridView。我尝试打印FlowLayoutPanel以及GridView,但在任何一种情况下,由垂直滚动条模糊的数据都不会被打印。在线上花了几个小时并尝试使用VB powerpacks后,我无所适从。

以下是GridView中数据的外观。

enter image description here

1 个答案:

答案 0 :(得分:0)

这是一个链接,您可以在其中尝试下载的代码示例
How To Print a Data Grid in C# and .NET