c#尽管有很多数据,但没有打印任何内容
本文关键字:打印 任何内 多数据 | 更新日期: 2023-09-27 18:20:56
我有一个网格视图,我想打印它。
这是我的代码:
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
循环结束时,结果仍然相同
发布的代码至少有三个问题:
- 在你的行循环中,你没有推进行计数器,这将导致一个无休止的循环
- 您也没有提前您的实际高度变量,这将导致套印所有行
- 由于这些都没有发生,因此不会调用printDocument1_PrintPage事件;您可能只是复制了示例中的代码,并没有将其与printDocument1连接起来
添加到循环的末尾:
rowCounter++;
realheight += height;
在构造函数中:
this.printDocument1.PrintPage += this.printDocument1_PrintPage;
解决这些问题至少应该打印出一些。。。