【问题标题】:c# nothing printed though there are many datac# 虽然有很多数据,但什么也没打印
【发布时间】:2014-09-06 11:24:14
【问题描述】:

我有一个网格视图,我想打印它。

这是我的代码:

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) {
            int rowCounter = 0;
            int z = 0;
            StringFormat str = new StringFormat();
            str.Alignment = StringAlignment.Near;
            str.LineAlignment = StringAlignment.Center;
            str.Trimming = StringTrimming.EllipsisCharacter;

            int width = 500 / (GridView.Columns.Count - 2);
            int realwidth = 100;
            int height = 40;

            int realheight = 100;

            for (z = 0; z < GridView.Columns.Count - 1; z++)
            {
                e.Graphics.FillRectangle(Brushes.AliceBlue, realwidth, realheight, width, height);
                e.Graphics.DrawRectangle(Pens.Black, realwidth, realheight, width, height);

                e.Graphics.DrawString(GridView.Columns[z].HeaderText, GridView.Font, Brushes.Black, realwidth, realheight);

                realwidth = realwidth + width;
            }

            z = 0;
            realheight = realheight + height;
            while (rowCounter < GridView.Rows.Count)
            {
                realwidth = 100;
                e.Graphics.FillRectangle(Brushes.AliceBlue, realwidth, realheight, width, height);
                e.Graphics.DrawRectangle(Pens.Black, realwidth, realheight, width, height);

                e.Graphics.DrawString(GridView.Rows[rowCounter].Cells[0].Value.ToString(), GridView.Font, Brushes.Black, realwidth, realheight);
                realwidth = realwidth + width;
            }

            printDialog1.Document = printDocument1;
            printDialog1.ShowDialog();
        }

当使用点击打印按钮时,我这样做:

DialogResult result = printDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                this.printDocument1.Print();
            }

加上在表单的构造中,我这样初始化打印变量:

this.printDocument1 = new System.Drawing.Printing.PrintDocument();
            this.printDialog1 = new System.Windows.Forms.PrintDialog();

我的问题是,当我点击打印时,虽然网格视图有超过 320 行,但我得到了空白页

更新 1

我正在关注这个教程http://code.msdn.microsoft.com/windowsdesktop/CSWinFormPrintDataGridView-75864c45

更新 2

网格视图变量是GridView

我猜代码很简单

更新 3

我加了

++rowCounter;
realheight = realheight + height;

while 循环的末尾添加广告,结果仍然相同

【问题讨论】:

  • 在Rows-Loop中,你不应该提高高度吗?和行计数器?
  • @TaW 我正在尝试你的想法,
  • @TaW 你的意思是在打印函数末尾加上++rowCounter; realheight = realheight + height; 吗?
  • 我想是的。我没有仔细检查,但发现循环有点奇怪。实际上它不应该完成,但页面也不应该都是空的,至少标题应该出现..
  • @TaW 是的,至少是标题,这就是我感到震惊的原因。另外,我测试了添加这两行代码,但结果仍然相同。

标签: c# winforms gridview printing


【解决方案1】:

您发布的代码至少存在三个问题:

  • 在您的行循环中,您不推进行计数器,这将导致无限循环。
  • 您也没有提前您的 realheight 变量,这将导致叠印所有行
  • 因为这一切都没有发生,所以您的 printDocument1_PrintPage 事件不会被调用;您可能只是从示例中复制了代码,并没有真正将它与 printDocument1 挂钩。

添加到循环的末尾:

rowCounter++;
realheight += height;

在构造函数中:

this.printDocument1.PrintPage += this.printDocument1_PrintPage;

解决这些问题至少应该打印出一些东西......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-21
    • 1970-01-01
    • 2016-06-12
    • 2015-09-09
    • 2021-12-23
    相关资源
    最近更新 更多