[英]Print Multiple Pages from SqlServer Database using C# PrintDocuments
i have a database table having more then 300 records i want to print it using PrintDocument/PrintPreview but the problem is only one page is showing in printPreview the remaining records are missing my Code is below please help me.我有一个包含 300 多条记录的数据库表,我想使用 PrintDocument/PrintPreview 打印它,但问题是 printPreview 中只显示一页,其余记录丢失了我的代码在下面,请帮助我。
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
int valueYPos=80;
int valueXPos=20
string classId=1;
con.SqlQuery("select [RollNo],[Name] from [dbo].[StudentRegistration] where [ClassId]='"
+ classId + "'");
con.ExQuery();
foreach (DataRow item in con.ExQuery().Rows)
{
e.Graphics.DrawString("RollNo" +item[0].ToString(), new Font("Arial", 20,
FontStyle.Bold), Brushes.Black, new Point(valueXPos, valueYPos));
e.Graphics.DrawString("RollNo" +item[1].ToString(), new Font("Arial", 20,
FontStyle.Bold), Brushes.Black, new Point(valueXPos+20, valueYPos));
valueYPos +=20;
}
}
private void button1_Click(object sender, EventArgs e)
{
PrintPreviewDialog pPD = new PrintPreviewDialog();
pPD.Document = printDocument1;
pPD.ShowDialog();
}
For print preview dialog see this answer Showing Print Preview in C#对于打印预览对话框,请参阅此答案Showing Print Preview in C#
First of all you can not access data like that because it will try to access data when it try to print each page.首先,您不能像那样访问数据,因为它会在尝试打印每一页时尝试访问数据。
private int pageNo = 1;
private static void Print_Invoice(object sender, PrintPageEventArgs e)
{
int valueYPos=80;
int valueXPos=20
string classId=1;
con.SqlQuery("select [RollNo],[Name] from [dbo].[StudentRegistration] where [ClassId]='"+ classId + "'");
con.ExQuery();
switch (pageNo)
{
case 1:
//Print some Data
e.HasMorePages = true;
break;
case 2:
//Print some Data
e.HasMorePages = true;
break;
case 3:
//Print some Data
e.HasMorePages = false;
break;
default:
e.HasMorePages = false;
break;
}
pageNo++;
}
Hope resolved your problem希望解决了你的问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.