我想在一个页面中打印20行。我从每个循环使用数据表中获取数据。
如果行小于20,一切正常。如果行大于20,则代码无法打印第二页。
请查看下面的代码,以帮助我找到解决方案。
int CurrentRecord = 0;
int RecordsPerPage = 20; // twenty items in a page
decimal Amount = 0;
bool StopReading = false;
foreach (DataRow row in table.Rows)
{
string ItemCode = row["ItemCode"].ToString();
g.DrawString(ItemCode, InvoiceFont, BlackBrush, xProductID, CurrentY);
string PartName = row["PartName"].ToString();
if (PartName.Length > 20)
PartName = PartName.Remove(20);
g.DrawString(PartName, InvoiceFont, BlackBrush, xProductName, CurrentY);
string Id = row["Identification"].ToString();
g.DrawString(Id, InvoiceFont, BlackBrush, xId, CurrentY);
string Size = row["Sizes"].ToString();
g.DrawString(Size, InvoiceFont, BlackBrush, xSize, CurrentY);
string Chasis = row["Chasis"].ToString();
if (Chasis.Length > 6)
Chasis = Chasis.Remove(7);
g.DrawString(Chasis, InvoiceFont, BlackBrush, xChasis, CurrentY);
string Qty = row["Qty"].ToString();
g.DrawString(Qty, InvoiceFont, BlackBrush, xQty, CurrentY);
string Rate = row["Rate"].ToString();
g.DrawString(String.Format("{0:0.0}", Rate), InvoiceFont, BlackBrush, xPrice, CurrentY);
//string t =Convert.ToInt32(row["Total"].ToString());
string Total = row["Total"].ToString();
// FieldValue = String.Format("{0:0.00}", Amount);
int xAmount = AmountPosition + (int)g.MeasureString("Price", InvoiceFont).Width;
xAmount = xAmount - (int)g.MeasureString("Total", InvoiceFont).Width;
// txtTotal.Text = Convert.ToInt32 (xAmount).ToString("N");
g.DrawString(Total, InvoiceFont, BlackBrush, xAmount, CurrentY);
CurrentY = CurrentY + InvoiceFontHeight;
CurrentRecord++;
if (CurrentRecord > RecordsPerPage)
{
e.HasMorePages = true;
CurrentRecord = 0;
// CurrentY = 20;
}
else
{
e.HasMorePages = false;
}
}
答案 0 :(得分:0)
在表单级别声明变量:
int printRow = 0;
在打印之前或在BeingPrint事件中,将printRow的值设置为零。
在这种情况下,您不能使用ForEach,因为每次打印页面时都需要跟踪索引位置:
int recordsPerPage = 20;
int rowsPrinted = 0;
for (; printRow < table.Rows.Count; printRow++) {
DataRow row = table.Rows[printRow];
// print code
rowsPrinted++;
if (rowsPrinted >= recordsPerPage && printRow < table.Rows.Count - 1) {
printRow++;
e.HasMorePages = true;
break;
}
}