导出到打印在我的页面中不起作用

本文关键字:不起作用 我的 打印 | 更新日期: 2023-09-27 18:26:59

我在c#中打印多个页面时遇到问题。我尝试了所有的可能性,但还没有解决。我的问题是,我想每页打印一行,但它没有这样做。目前它只打印所有页面上的第一行。这是代码在PrintPageEvent 中

for (int i = 0; i < dt.Rows.Count; i++)
{
string cell = dt.Rows[i][0].ToString();
// string itemx = dr["item_id"].ToString().PadLeft(6,'0'); 
string itemx = cell.PadLeft(6, '0');
string item2 = "*" + itemx + "*";
string item3 = "*UMS" + itemx + "*";
e.Graphics.DrawString(itemx, 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);
// yValue = yValue + 70;
// yValue2 = yValue2 + 70;
if (totalnumberA < dt.Rows.Count)
{
e.HasMorePages = true;
totalnumberA++; //return;
//eturn;
}
else
{
e.HasMorePages = false;
//totalnumberA++; //return;
}
}

我的按钮事件是这个

try
{
DBConnection DB = new DBConnection();
DB.cnTransact.Open();
string sql = "select * from tbl_items where serialNo='122'";
cm = new SqlCommand(sql, DB.cnTransact);
SqlDataAdapter people = new SqlDataAdapter(cm);
people.Fill(dt);
 //foreach (DataRow dr in dt.Rows) 
}

catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
}
totalnumberA = 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(); - See more at: 

导出到打印在我的页面中不起作用

对于文档中打印的每一页都会触发PrintPage事件。因此,在每一页的代码中,您都会开始一个新的或循环,该循环初始化为零,最终打印第一行。因此,删除for循环,并使用totalNumberA作为行的索引。

string cell = dt.Rows[totalnumberA][0].ToString();
string itemx = cell.PadLeft(6, '0');
string item2 = "*" + itemx + "*";
string item3 = "*UMS" + itemx + "*";
e.Graphics.DrawString(itemx, 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 (totalnumberA < dt.Rows.Count)
{
    e.HasMorePages = true;
    totalnumberA++;
}
else
{
    e.HasMorePages = false;
}