C#打印自动分页

liangzhl 2009-12-10 08:39:07
我的是在winform窗口中放了个datagridview,要把里面的数据打印出来同时都是放在表格里的,我是自己画表格的,但是如果多出一页就不知道怎么处理了,求助????

代码如下:
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Font font = new Font("Tahoma", 10, FontStyle.Regular);//设置画笔
Font font2 = new Font("宋体", 25, FontStyle.Bold);
Brush bru = Brushes.Blue;
Pen pen = new Pen(bru);
Graphics g = e.Graphics;
pen.Width = 1;
//设置各边距
float linesPerPage = 0;
//linesPerPage = e.MarginBounds.Height / 20;

int nLeft = e.PageSettings.Margins.Left / 2;
int nTop = e.PageSettings.Margins.Top - e.PageSettings.Margins.Left + 10;
int nRight = e.PageSettings.Margins.Right / 2;
int nBottom = e.PageSettings.Margins.Bottom;
int nWidth = e.PageSettings.PaperSize.Width - nRight - nLeft;
int nHeight = e.PageSettings.PaperSize.Height - nTop - nBottom;

g.DrawString(cmbcusname.SelectedItem.ToString() + dateTimePicker1.Value.Month.ToString() + "月份" + label5.Text, font2, bru, nLeft + 200, nTop + 50);

e.Graphics.DrawLine(pen, nLeft + 534, nTop + 95, nLeft + nWidth, nTop + 95);
e.Graphics.DrawLine(pen, nLeft + 534, nTop + 95, nLeft + 534, nTop + 130);
g.DrawString("贷款金额为:" + txtPrice.Text.Trim(), font, bru, nLeft + 535, nTop + 105);
e.Graphics.DrawLine(pen, nLeft + nWidth, nTop + 95, nLeft + nWidth, nTop + 130);

e.Graphics.DrawLine(pen, nLeft, nTop + 130, nLeft + nWidth, nTop + 130);
e.Graphics.DrawLine(pen, nLeft, nTop + 130, nLeft, nTop + 160);
g.DrawString("编号", font, bru, nLeft + 5, nTop + 135);
e.Graphics.DrawLine(pen, nLeft + 95, nTop + 130, nLeft + 95, nTop + 160);
g.DrawString("订单号", font, bru, nLeft + 105, nTop + 135);
e.Graphics.DrawLine(pen, nLeft + 195, nTop + 130, nLeft + 195, nTop + 160);
g.DrawString("货号", font, bru, nLeft + 205, nTop + 135);
e.Graphics.DrawLine(pen, nLeft + 295, nTop + 130, nLeft + 295, nTop + 160);
g.DrawString("材质", font, bru, nLeft + 305, nTop + 135);
e.Graphics.DrawLine(pen, nLeft + 410, nTop + 130, nLeft + 410, nTop + 160);
g.DrawString("规格", font, bru, nLeft + 420, nTop + 135);
e.Graphics.DrawLine(pen, nLeft + 534, nTop + 130, nLeft + 534, nTop + 160);
g.DrawString("单位", font, bru, nLeft + 535, nTop + 135);
e.Graphics.DrawLine(pen, nLeft + 575, nTop + 130, nLeft + 575, nTop + 160);
g.DrawString("数量", font, bru, nLeft + 580, nTop + 135);
e.Graphics.DrawLine(pen, nLeft + 615, nTop + 130, nLeft + 615, nTop + 160);
g.DrawString("单价", font, bru, nLeft + 620, nTop + 135);
e.Graphics.DrawLine(pen, nLeft + 665, nTop + 130, nLeft + 665, nTop + 160);
g.DrawString("金额", font, bru, nLeft + 670, nTop + 135);
e.Graphics.DrawLine(pen, nLeft + nWidth, nTop + 130, nLeft + nWidth, nTop + 160);
e.Graphics.DrawLine(pen, nLeft, nTop + 160, nLeft + nWidth, nTop + 160);

int hj = 165;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
e.Graphics.DrawLine(pen, nLeft, nTop + hj - 5, nLeft, nTop + hj + 22);
g.DrawString(dataGridView1.Rows[i].Cells[0].Value.ToString(), font, bru, nLeft + 5, nTop + hj + 4);
e.Graphics.DrawLine(pen, nLeft + 95, nTop + hj - 5, nLeft + 95, nTop + hj + 22);
g.DrawString(dataGridView1.Rows[i].Cells[1].Value.ToString(), font, bru, nLeft + 100, nTop + hj + 4);
e.Graphics.DrawLine(pen, nLeft + 195, nTop + hj - 5, nLeft + 195, nTop + hj + 22);
g.DrawString(dataGridView1.Rows[i].Cells[3].Value.ToString(), font, bru, nLeft + 200, nTop + hj + 4);
e.Graphics.DrawLine(pen, nLeft + 295, nTop + hj - 5, nLeft + 295, nTop + hj + 22);
g.DrawString(dataGridView1.Rows[i].Cells[2].Value.ToString(), font, bru, nLeft + 300, nTop + hj + 4);
e.Graphics.DrawLine(pen, nLeft + 410, nTop + hj - 5, nLeft + 410, nTop + hj + 22);
g.DrawString(dataGridView1.Rows[i].Cells[4].Value.ToString(), font, bru, nLeft + 415, nTop + hj + 4);
e.Graphics.DrawLine(pen, nLeft + 534, nTop + hj - 5, nLeft + 534, nTop + hj + 22);
g.DrawString(dataGridView1.Rows[i].Cells[5].Value.ToString(), font, bru, nLeft + 545, nTop + hj + 4);
e.Graphics.DrawLine(pen, nLeft + 575, nTop + hj - 5, nLeft + 575, nTop + hj + 22);
g.DrawString(dataGridView1.Rows[i].Cells[6].Value.ToString(), font, bru, nLeft + 580, nTop + hj + 4);
e.Graphics.DrawLine(pen, nLeft + 615, nTop + hj - 5, nLeft + 615, nTop + hj + 22);
g.DrawString(dataGridView1.Rows[i].Cells[7].Value.ToString(), font, bru, nLeft + 620, nTop + hj + 4);
e.Graphics.DrawLine(pen, nLeft + 665, nTop + hj - 5, nLeft + 665, nTop + hj + 22);
g.DrawString(dataGridView1.Rows[i].Cells[8].Value.ToString(), font, bru, nLeft + 670, nTop + hj + 4);

e.Graphics.DrawLine(pen, nLeft + nWidth, nTop + hj - 5, nLeft + nWidth, nTop + hj + 22);
//e.Graphics.DrawLine(pen, nLeft+nWidth, nTop + 130, nLeft +nWidth, nTop + hj + 30);
hj += 22;
e.Graphics.DrawLine(pen, nLeft, nTop + hj, nLeft + nWidth, nTop + hj);
}
}
...全文
491 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
liangzhl 2009-12-10
  • 打赏
  • 举报
回复
行数不是固定的,列数和宽度固定了,楼上的那你说应怎么解决啊?
an_ding 2009-12-10
  • 打赏
  • 举报
回复
自己画的话问题很多的,楼主你的行数是固定啊,如果某一个单元格的数据比较多,放不下是不是就显不出来了?不能换行吧?
xtiu77 2009-12-10
  • 打赏
  • 举报
回复
在for前面加

if(Rows.Count-index<pageRows)
{
pageRows=Rows.Count-index;
}

分页的思路就是这样.
liangzhl 2009-12-10
  • 打赏
  • 举报
回复
for (int i = 0; i < pageRows; i++)
//////////////////////////////////////
但是如果DAG中并没有这么多行,比如只有10行,那么pageRows是20,运行是会出错的,因为DAG中就只有10行啊,其他的就找不到了,而i还是会在继续加1。我是直接用DAG中的数据来填充的
难道要在加个IF来判断是否pageRows大于DAV.Rows.Count。如果大的话就用你说的FOR.如果小就用i<dav.rows.count???不过这样看起来很不那个 啊
liangzhl 2009-12-10
  • 打赏
  • 举报
回复
楼上的可以说得清楚些吗?
xtiu77 2009-12-10
  • 打赏
  • 举报
回复


if (barCodeStart <= barCodeEnd)//分页
{
e.HasMorePages = true;
}
else
{
e.HasMorePages = false;

}
liaoyukun111 2009-12-10
  • 打赏
  • 举报
回复
友情帮顶
simonezhlx 2009-12-10
  • 打赏
  • 举报
回复
你的意思是自动和纸张大小的匹配?为什么不用水晶报表
kaizi_sun 2009-12-10
  • 打赏
  • 举报
回复
count 是你每页打印行的记数
linesparpage是你每页要打的行数,自己大概计算下。
linNo是你dataGridView1读到的row index.
lineQty你总共需要打印的行数

while (count < linesPerPage && lineNo < lineQty)
{
if (lineNo < dataGridView1.Rows.Count) //由于lineNo用于表中的下标值,因此需要加判断
{
g.DrawString(dataGridView1.Rows[lineNo].Cells[1].Value.ToString(), font, bru, nLeft + 100, nTop + hj + 4); //这里是你的循环

count++;
}
lineNo++;
}
//下面分页要放在循环之外
if (lineQty > lineNo)
{
count = 0;
printingPageNo++;
e.HasMorePages = true;
return;
}
else
{
count++;
e.HasMorePages = false;
lineNo = 0;
lineQty = 0;
}
liangzhl 2009-12-10
  • 打赏
  • 举报
回复
我要的是直接就能打印出来的啊,水晶报表没用过,也不会用啊,所以就自己画了
ddsxd19 2009-12-10
  • 打赏
  • 举报
回复
自己画?
可以数据导在excel上再打印出来~
小坏猪猪 2009-12-10
  • 打赏
  • 举报
回复
lz用水晶报表吧
liangzhl 2009-12-10
  • 打赏
  • 举报
回复
8楼说的,还是不行啊。郁闷啊~~~~

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧