【问题标题】:e.graphics.drawstring showing text on printpreview but not printinge.graphics.drawstring 在 printpreview 上显示文本但不打印
【发布时间】:2013-04-19 09:24:04
【问题描述】:

我正在使用基于 c# windows 的应用程序。[Visual Studio 2010]

我正在尝试打印账单。

我已经使用e.Graphics.Drawstring 形成了该账单。

在这里,它向我展示了带有所有适当数据的打印预览。

但是,当我打印该 printpreview 时,它没有显示账单中的项目。[打印预览的特定部分。其他数据正在显示]。

我的代码如下:

e.Graphics.DrawString("Amount", drawFont, Brushes.Black, new RectangleF(tempX + 600, tempY, dataGridView1.Columns[0].Width, dataGridView1.Rows[1].Height), str);

    while (i < dataGridView1.Rows.Count-1)
            {

                if (height > e.MarginBounds.Height)
                {
                    height = 100;
                    width = 100;
                    e.HasMorePages = true;
                    return;
                }
                height += dataGridView1.Rows[i].Height;

                e.Graphics.DrawString((idInCol).ToString(), drawFont, Brushes.Black, new RectangleF(tempX, ((i*22)+(tempY+25)), dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height), str);


                e.Graphics.DrawString(dataGridView1.Rows[i].Cells[2].FormattedValue.ToString().Trim(), drawFont, Brushes.Black, new RectangleF(tempX + 30, ((i * 22) + (tempY +25)), 500, dataGridView1.Rows[0].Height), str);

                e.Graphics.DrawString(dataGridView1.Rows[i].Cells[3].FormattedValue.ToString(), drawFont, Brushes.Black, new RectangleF(tempX + 400, ((i * 22) + (tempY +25)), dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height), drawFormat);
                width += dataGridView1.Columns[0].Width;

                              e.Graphics.DrawString(dataGridView1.Rows[i].Cells[4].FormattedValue.ToString().Trim(), drawFont, Brushes.Black, new RectangleF(tempX + 500, ((i * 22) + (tempY +25)), dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height), drawFormat);
                width += dataGridView1.Columns[0].Width;

                               e.Graphics.DrawString(dataGridView1.Rows[i].Cells[5].FormattedValue.ToString().Trim(), drawFont, Brushes.Black, new RectangleF(tempX + 600, ((i * 22) + (tempY +25)), dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height), drawFormat);
                width += dataGridView1.Columns[0].Width;
                idInCol++;
                i++;
            }

这是我的账单截图:

当我打印出来时,它没有显示账单详情。除了那个之外,所有其他数据都显示在打印输出中。

我已粘贴代码(while 循环),我从中获取账单详情(项目、序列号等)。

我的代码有什么遗漏吗?

我在代码中做错了吗?

请帮帮我。

【问题讨论】:

  • 代码中明显缺少的是 BeginPrint 事件处理程序。需要将“i”变量重置回 0。

标签: c# .net winforms printing


【解决方案1】:

我知道这已经两个月了,您可能发现了错误或放弃了。

无论如何,我走了:这里我假设变量i 被声明为全局类。如果是这种情况,您需要在 while 循环完成后将 i 重置为 0。

要确认您需要重置i,只需调试您的应用程序并在您第二次进入该方法(真正的打印方法)时检查i 值。此外,如果您直接打印而不进行预览,它应该可以工作。

【讨论】:

  • 帮助了我。 +1。我添加了一个开始打印事件并将变量初始化为 0。这解决了问题。我正在为这个答案开始赏金。
  • @pkkg 为什么你开始赏金,当你解决了你的问题。赏金的目的是吸引未解决问题的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
  • 2010-12-23
  • 2021-09-26
相关资源
最近更新 更多