在下面的打印预览代码中,它显示来自DataGridView的所有数据,但不打印所有数据只打印页面结构
我附上了打印预览和打印输出的图像
private void btnPrintPreview_Click(object sender, EventArgs e)
{
printPreviewDialog1.ShowDialog();
i = 0;
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawString("Purchase Order ", new Font("Arial", 28, FontStyle.Bold), Brushes.Black, new Point(280, 10));
e.Graphics.DrawString("POPULAR ", new Font("Arial", 20, FontStyle.Bold), Brushes.Black, new Point(10, 50));
e.Graphics.DrawString("Center", new Font("Arial", 14, FontStyle.Regular), Brushes.Black, new Point(20, 80));
e.Graphics.DrawString("Behind ,", new Font("Arial", 11, FontStyle.Regular), Brushes.Black, new Point(10, 100));
e.Graphics.DrawString(" Colony,", new Font("Arial", 11, FontStyle.Regular), Brushes.Black, new Point(10, 120));
e.Graphics.DrawString("Sh - 501.", new Font("Arial", 11, FontStyle.Regular), Brushes.Black, new Point(10, 140));
e.Graphics.DrawString("TIN : " + DateTime.Now.ToShortDateString(), new Font("Arial", 11, FontStyle.Regular), Brushes.Black, new Point(10, 160));
e.Graphics.DrawString("GST : " + DateTime.Now.ToShortDateString(), new Font("Arial", 11, FontStyle.Regular), Brushes.Black, new Point(10, 178));
e.Graphics.DrawString("P O No. : " + lblPONumber.Text.Trim(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(10, 197));
e.Graphics.DrawString("Date : " + date.Text, new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(506, 197));
e.Graphics.DrawString("Customer Name : " + txtSupplierName.Text.Trim(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(420, 70));
e.Graphics.DrawString("Company Name : " + txtSupplierCompanyName.Text.Trim(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(420, 90));
e.Graphics.DrawString("Mobile No. : " + txtMobile.Text.Trim(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(420, 110));
e.Graphics.DrawString("Address : ", new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(420, 130));
e.Graphics.DrawString(" ", new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(420, 150));
e.Graphics.DrawString("City : ", new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(420, 170));
e.Graphics.DrawString("------------------------------------------------------------------------------------------------------------------------------------", new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(10, 210));
e.Graphics.DrawString("Sl No.", new Font("Arial", 10, FontStyle.Regular), Brushes.Black, new Point(05, 227));
e.Graphics.DrawString("Item Name", new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(55, 225));
e.Graphics.DrawString("Quantity", new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(380, 225));
e.Graphics.DrawString("Unit Price", new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(510, 225));
e.Graphics.DrawString("Total", new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(660, 225));
e.Graphics.DrawString("------------------------------------------------------------------------------------------------------------------------------------", new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(10, 235));
int ypos = 250;
while (i < dataGridView1.Rows.Count)
{
if (ypos > e.MarginBounds.Height)
{
ypos = 250;
e.HasMorePages = true;
return;
}
numberofitemsprinted++;
SlNumber++;
e.Graphics.DrawString(SlNumber.ToString(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(10, ypos));
e.Graphics.DrawString(dataGridView1.Rows[i].Cells[0].FormattedValue.ToString(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(60, ypos));
e.Graphics.DrawString(dataGridView1.Rows[i].Cells[3].FormattedValue.ToString(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(397, ypos));
e.Graphics.DrawString(dataGridView1.Rows[i].Cells[2].FormattedValue.ToString(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(530, ypos));
e.Graphics.DrawString(dataGridView1.Rows[i].Cells[4].FormattedValue.ToString(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(670, ypos));
i++;
ypos = ypos + 30;
}
e.Graphics.DrawString("------------------------------------------------------------------------------------------------------------------------------------", new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(10, ypos));
e.Graphics.DrawString("Total Amount : " + txtTotalSum.Text.Trim(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(510, ypos + 30));
e.Graphics.DrawString("Amount in Words : " + txtTotalSumWords.Text.Trim(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(200, ypos + 120));
numberofitemsperpage = 0;
numberofitemsprinted = 0;
SlNumber = 0;
}
private void btnPrint_Click(object sender, EventArgs e)
{
printDocument1.Print();
}
答案 0 :(得分:0)
您需要在打印前重置变量i
。
看起来PrintPage
被称为一次用于预览,然后,没有重置i
,再次用于实际打印..
这应该有所帮助:
private void btnPrint_Click(object sender, EventArgs e)
{
i = 0;
printDocument1.Print();
}
我还强烈建议将名称i
更改为更明确的内容,例如currentPrintRow
..