c#打印对话框不打印所有页面 [英] c# Print Dialog not printing all pages

查看:140
本文介绍了c#打印对话框不打印所有页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好

我想使用c#中的打印对话框打印数据库中的值。

我想在页面上打印打印1值,但它不起作用。它只打印2页,然后在第一页打印所有值。请告诉我该怎么办。

我的代码是



在打印页面事件

PAPERSIZE PAPERSIZE =新PaperSize(papersize,200,100); //页面大小约2X1英寸

int totalnumber2 = 0;



public void PrintDocumentOnPrintPage(object sender,PrintPageEventArgs e)

{

SqlCommand cm;

数据表DT =新数据表();

尝试

{

DBConnection的DB =新DBConnection的();

DB.cnTransact.Open();

string sql =select * from tbl_items;

cm = new SqlCommand(sql,DB.cnTransact);

SqlDataAdapter people = new SqlDataAdapter(cm);

people.Fill(dt);

int xValue = 25;

int yValue = 30;

int yValue2 = 65;

float pageheight = e.MarginBounds.Height;

int x = 0 ;

foreach(DataRow dr in dt.Rows)

{

string itemx = dr [item_id]。ToString()。PadLeft (6, '0'); ;

string item2 =*+ itemx +*;

string item3 =* UMS+ itemx +*;

e.Graphics.DrawString(item2,new Font(Free 3 of 9,30,FontStyle.Regular),Brushes.Black,xValue,yValue);

e.Graphics.DrawString(item3 ,new Font(Courier New,14,FontStyle.Regular),Brushes.Black,xValue,yValue2);

if(totalnumber2< dt.Rows.Count)

{

e.HasMorePages = true;

}

else {

e.HasMorePages = false;

}

totalnumber2 ++;

}

}

catch(Exception ex)

{

MessageBox.Show(ex.Message);

}

}



按钮点击事件



private void btn1_Click(object sender,EventArgs e)

{

totalnumber2 = 0;

PrintDialog dialog = new PrintDialog();

PrintDocument printDocument = new PrintDocument();

dialog.Document = printDocument;

printDocument.DefaultPageSettings.PaperSize = paperSize;

dialog.ShowDialog();

printDocument.PrintPage + = PrintDocumentOnPrintPage;

printDocument.DocumentName =条形码;

printDocument.Print();

}



请告诉我我在哪里犯错误并帮助我解决这个问题。

谢谢

Hello
i want to print Values from database using print dialog in c#.
i want to print print 1 value at a page but it is not working. It only prints 2 pages and then Prints all values at first page. Please tell me what should i do with this.
My code is

At Print Page Event
PaperSize paperSize = new PaperSize("papersize", 200, 100); //page size 2X1 inches approx
int totalnumber2 = 0;

public void PrintDocumentOnPrintPage(object sender, PrintPageEventArgs e)
{
SqlCommand cm;
DataTable dt = new DataTable();
try
{
DBConnection DB = new DBConnection();
DB.cnTransact.Open();
string sql = "select * from tbl_items";
cm = new SqlCommand(sql, DB.cnTransact);
SqlDataAdapter people = new SqlDataAdapter(cm);
people.Fill(dt);
int xValue = 25;
int yValue = 30;
int yValue2 = 65;
float pageheight = e.MarginBounds.Height;
int x = 0;
foreach (DataRow dr in dt.Rows)
{
string itemx = dr["item_id"].ToString().PadLeft(6,'0'); ;
string item2 = "*"+itemx+"*";
string item3 = "*UMS" + itemx + "*";
e.Graphics.DrawString(item2, new Font("Free 3 of 9", 30, FontStyle.Regular), Brushes.Black, xValue, yValue);
e.Graphics.DrawString(item3, new Font("Courier New", 14, FontStyle.Regular), Brushes.Black, xValue, yValue2);
if (totalnumber2 < dt.Rows.Count)
{
e.HasMorePages =true;
}
else {
e.HasMorePages = false;
}
totalnumber2++;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

At Button Click Event

private void btn1_Click(object sender, EventArgs e)
{
totalnumber2 = 0;
PrintDialog dialog = new PrintDialog();
PrintDocument printDocument = new PrintDocument();
dialog.Document = printDocument;
printDocument.DefaultPageSettings.PaperSize = paperSize;
dialog.ShowDialog();
printDocument.PrintPage += PrintDocumentOnPrintPage;
printDocument.DocumentName = "Barcodes";
printDocument.Print();
}

Please tell me where i am doing mistakes and help me in solving this.
Thanks

推荐答案

PrintPage方法被称为一次每个页面打印,直到e.HasMorPages设置为false - 这意味着您的整个数据检索例程正在每个页面上执行....



你需要在此方法之外获取数据,仅打印适合当前页面的记录,并在每次打印记录时更新当前记录指针。



检查出这个:绝对新手的.NET打印指南 [<一个href =http://www.codeproject.com/Articles/27404/An-absolute-beginner-s-guide-to-printing-in-NETtarget =_ blanktitle =New Window> ^ ](它也适用于C#)
The PrintPage method is called once for each page printed until e.HasMorPages is set to false - which means your whole data retrieval routine is being executed on each page....

You need to get your data outside this method, print only the records that fit on the current page, and update the current record pointer each time a record is printed.

Check out this: An absolute beginner's guide to printing in .NET[^] (it also applies to C#)


这篇关于c#打印对话框不打印所有页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆