首页 > 解决方案 > 如何打印每行打印在新页面中的datagridview?

问题描述

我有一个包含信息的 datagridview 表

a | b
--+--
3 | 3
4 | 4
5 | 5

如何使用 c# 中的打印功能在第一页打印 3,3,在第二页打印 4,4,在第三页打印 5,5,依此类推?

    private void printDocTicket_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        float point = 60;

        for (int x = 0; x < DGVUpload.Rows.Count; x++)
        {
            point += 60;
            e.Graphics.DrawString(DGV["Sym3", x].Value.ToString(), new Font("Courier New", 30), Brushes.Black, 60 , point );
            e.Graphics.DrawString(DGV["Sym4", x].Value.ToString(), new Font("Courier New", 30), Brushes.Black, 250, point);

            if (x <= DGV.Rows.Count)
            {
                e.HasMorePages = false;
            }
            else
            {
                e.HasMorePages = true;
            }
        }
    }

标签: c#printingdatagridview

解决方案


在意识到 e.HasMorePages 重新启动 PrintPage 事件后修改我的代码给了我正在寻找的结果。

int i;

private void printDocTicket_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    float point = 60;  

    e.Graphics.DrawString(DGV["Sym3", x].Value.ToString(), new Font("Courier New", 30), Brushes.Black, 60 , point );
    e.HasMorePages = true;
    i++;

    if(i == DGV.Rows.Count)
    {
        e.HasMorePages = false;
    }
}

推荐阅读