2014-10-19 70 views
0

我在使用c#打印多个页面时遇到问题。我尝试了所有可能性,但尚未修复。我的问题是,我想每页打印1行,但它不这样做。目前它只在所有页面上打印第一行。 。下面是代码 在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: 

回答

1

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; 
}