2014-10-19 21 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; 
}