winform 打印预览 无法分页
用e.HasMorePages = true 控制分页,但是每页只显示一条就可以分页,如果控制为每页显示三条,无法分页。代码如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using xsgl;
namespace winTest
{
public partial class Form1 : Form
{
Class1 obj = new Class1();
DataSet ds = new DataSet();
int _curRow=0;
public Form1()
{
InitializeComponent();
this.printDialog1.Document = this.printDocument1;//必要的
this.printPreviewDialog1.Document = this.printDocument1;
this.pageSetupDialog1.Document = this.printDocument1;
}
private void Form1_Load(object sender, EventArgs e)
{
string sql1 = "select * from zm where zh<>12";
ds = obj.SelectSqlRows(sql1, "zm");
DataTable dt = ds.Tables[0];
for (int i = 0; i < dt.Rows.Count; i++)
{
string zm = dt.Rows[i]["zm"].ToString();
toolStripComboBox1.Items.Add(zm.ToString());
}
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//双击printDocument控件,这才是真正开始干活的,在这里面写你想要打印信息
Font font = new Font("Tahoma", 12, FontStyle.Regular);//设置画笔
Brush bru = Brushes.Black;
Pen pen = new Pen(bru);
pen.Width = 1;
//设置各边距
int nLeft = this.pageSetupDialog1.PageSettings.Margins.Left;
int nTop = this.pageSetupDialog1.PageSettings.Margins.Top;
int nRight = this.pageSetupDialog1.PageSettings.Margins.Right;
int nBottom = this.pageSetupDialog1.PageSettings.Margins.Bottom;
int nWidth = this.pageSetupDialog1.PageSettings.PaperSize.Width - nRight - nLeft;
int nHeight = this.pageSetupDialog1.PageSettings.PaperSize.Height - nTop - nBottom;
//打印各边距
int x = 0;
int y = -1;
for (int i = _curRow; i <= 9; i++)
{
if (i % 3 == 0)
{
x = 0;
}
else
{
x++;
}
if (i % 3 == 0)
{
y++;
}
else
{
}
DataTable dt = ds.Tables[0];
e.Graphics.DrawRectangle(pen, nLeft + x * 270, nTop + y * 520, 250, 500);
e.Graphics.DrawString(dt.Rows[i]["zm"].ToString(), font, bru, nLeft + x * 270, nTop + y * 520 + 20);//如果要打印datagridView在这里遍历便可
e.Graphics.DrawString("部门", font, bru, nLeft + x * 270, nTop + y * 520 + 40);//如果要打印datagridView在这里遍历便可
e.Graphics.DrawString("单位", font, bru, nLeft + x * 270 + 50, nTop + y * 520 + 40);//内容在现有的基础上加50
e.Graphics.DrawString("序号", font, bru, nLeft + x * 270, nTop + y * 520 + 60);//如果要打印datagridView在这里遍历便可
e.Graphics.DrawString("部门", font, bru, nLeft + x * 270 + 50, nTop + y * 520 + 60);//内容在现有的基础上加50
if (_curRow == 3)
{
e.HasMorePages = false;
}
else
{
if(i % 3==0)
{
e.HasMorePages = true;
}
}
_curRow++;
}
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
this.printPreviewDialog1.ShowDialog(); //打印预览
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
this.pageSetupDialog1.ShowDialog(); //打印设置
}
private void toolStripButton3_Click(object sender, EventArgs e)
{
if (this.printDialog1.ShowDialog() == DialogResult.OK)//打印
this.printDocument1.Print();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
//双击printDocument控件,这才是真正开始干活的,在这里面写你想要打印信息
Font font = new Font("Tahoma", 12, FontStyle.Regular);//设置画笔
Brush bru = Brushes.Black;
Pen pen = new Pen(bru);
pen.Width = 1;
//设置各边距
int nLeft = this.pageSetupDialog1.PageSettings.Margins.Left;
int nTop = this.pageSetupDialog1.PageSettings.Margins.Top;
int nRight = this.pageSetupDialog1.PageSettings.Margins.Right;
int nBottom = this.pageSetupDialog1.PageSettings.Margins.Bottom;
int nWidth = this.pageSetupDialog1.PageSettings.PaperSize.Width - nRight - nLeft;
int nHeight = this.pageSetupDialog1.PageSettings.PaperSize.Height - nTop - nBottom;
//打印各边距
int x = 0;
int y = -1;
for (int i = 0; i <= 9; i++)
{
if (i % 3 == 0)
{
x = 0;
}
else
{
x++;
}
if (i % 3 == 0)
{
y++;
}
else
{
}
DataTable dt = ds.Tables[0];
e.Graphics.DrawRectangle(pen, nLeft + x * 270, nTop + y * 520, 250, 500);
e.Graphics.DrawString(dt.Rows[i]["zm"].ToString(), font, bru, nLeft + x * 270, nTop + y * 520 + 20);//如果要打印datagridView在这里遍历便可
e.Graphics.DrawString("部门", font, bru, nLeft + x * 270, nTop + y * 520 + 40);//如果要打印datagridView在这里遍历便可
e.Graphics.DrawString("单位", font, bru, nLeft + x * 270 + 50, nTop + y * 520 + 40);//内容在现有的基础上加50
e.Graphics.DrawString("序号", font, bru, nLeft + x * 270, nTop + y * 520 + 60);//如果要打印datagridView在这里遍历便可
e.Graphics.DrawString("部门", font, bru, nLeft + x * 270 + 50, nTop + y * 520 + 60);//内容在现有的基础上加50
}
}
}
} --------------------编程问答-------------------- 代码太多,占个位,帮顶一个 --------------------编程问答--------------------
if (_curRow == 3)
{
e.HasMorePages = false;
}
这个是不是有问题 --------------------编程问答-------------------- 我直接写死的分三页。 --------------------编程问答-------------------- 加断点,测试了,但是就是不知道为什么e.HasMorePages = true 不起作用。 --------------------编程问答-------------------- for (int i = _curRow; i <= 9; i++)
if (_curRow == 3)
{
e.HasMorePages = false;
}
else
{
if(i % 3==0)
{
e.HasMorePages = true;
}
}
_curRow++;
}
}
这些代码都有问题,好好的想一下吧 --------------------编程问答-------------------- if (_curRow == 3)
这块应该是9 --------------------编程问答-------------------- 好牛 --------------------编程问答-------------------- 一眼就能看出是逻辑判断问题
补充:.NET技术 , C#