I am printing invoice using PrintDialogue and PrintDocument. Invoice details are handling using Data Grid View.Data Grid View is iterating using for..each statement.I have already set e.hasMorePages into true.But printing invoice rewriting same pages.Not came in separate pages.Please help me to print in separate pages.
void pdoc_PrintPage(object sender, PrintPageEventArgs e)
{
Graphics graphics = e.Graphics;
Font font = new Font("Courier New", 10);
float fontHeight = font.GetHeight();
int startxsupp = 65;
int startysupp = 124;
int offsetsupp = 40;
graphics.DrawString(txtSupp.Text, new Font("Courier New", 12, FontStyle.Bold),
new SolidBrush(Color.Black), startxsupp, startysupp + offsetsupp);
offsetsupp = offsetsupp + 80;
int startxsuppadd = 60;
graphics.DrawString(txtSuppAddress.Text, new Font("Courier New", 12, FontStyle.Bold),
new SolidBrush(Color.Black), startxsuppadd, startysupp + offsetsupp);
int startX = 660;
int startY = 120;
int Offset = 40;
graphics.DrawString(txtInvoiceNo.Text, new Font("Courier New", 12, FontStyle.Bold),
new SolidBrush(Color.Black), startX, startY + Offset);
Offset = Offset + 40;
string formattedInvDate = dtpInvDate.Value.ToString("dd-MM-yyyy");
graphics.DrawString(formattedInvDate,
new Font("Courier New", 12, FontStyle.Bold),
new SolidBrush(Color.Black), startX, startY + Offset);
Offset = Offset + 35;
graphics.DrawString(txtDONo.Text.ToString(),
new Font("Courier New", 12, FontStyle.Bold),
new SolidBrush(Color.Black), startX, startY + Offset);
Offset = Offset + 35;
string formattedDoDate = dtpDOdate.Value.ToString("dd-MM-yyyy");
graphics.DrawString(formattedDoDate,
new Font("Courier New", 12, FontStyle.Bold),
new SolidBrush(Color.Black), startX, startY + Offset);
Offset = Offset + 40;
graphics.DrawString(txtLPONo.Text.ToString(),
new Font("Courier New", 12, FontStyle.Bold),
new SolidBrush(Color.Black), startX, startY + Offset);
Offset = Offset + 40;
int startxgrid = 60;
int startygrid = 360;
int offsetgridy = 40;
foreach (DataGridViewRow dr in gridInvDetail.Rows)
{
if ((dr.Cells["SLNO"].Value ?? string.Empty).ToString() != null)
{
string col1 = (dr.Cells["SLNO"].Value ?? string.Empty).ToString();
string col2 = (dr.Cells["DESC"].Value ?? string.Empty).ToString();
string col3 = (dr.Cells["Unit"].Value ?? string.Empty).ToString();
string col4 = (dr.Cells["Quantity"].Value ?? string.Empty).ToString();
string col5 = (dr.Cells["Rate"].Value ?? string.Empty).ToString();
string col6 = (dr.Cells["Amount"].Value ?? string.Empty).ToString();
startxgrid = 30;
graphics.DrawString(col1,
new Font("Courier New", 12, FontStyle.Bold),
new SolidBrush(Color.Black), startxgrid, startygrid + offsetgridy);
startxgrid = 60;
graphics.DrawString(col2,
new Font("Courier New", 12, FontStyle.Bold),
new SolidBrush(Color.Black), startxgrid, startygrid + offsetgridy);
startxgrid = 510;
graphics.DrawString(col3,
new Font("Courier New", 12, FontStyle.Bold),
new SolidBrush(Color.Black), startxgrid, startygrid + offsetgridy);
startxgrid = 590;
graphics.DrawString(col4,
new Font("Courier New", 12, FontStyle.Bold),
new SolidBrush(Color.Black), startxgrid, startygrid + offsetgridy);
startxgrid = 655;
graphics.DrawString(string.Format("{0:#,##0.00}", double.Parse(col5)),
new Font("Courier New", 12, FontStyle.Bold),
new SolidBrush(Color.Black), startxgrid, startygrid + offsetgridy);
startxgrid = 750;
graphics.DrawString(string.Format("{0:#,##0.00}", double.Parse(col6)),
new Font("Courier New", 12, FontStyle.Bold),
new SolidBrush(Color.Black), startxgrid, startygrid + offsetgridy);
offsetgridy = offsetgridy + 20;
if ((startygrid + offsetgridy) > 900)
{
e.HasMorePages = true;
offsetgridy = 40;
}
}
else
{
break;
}
}
e.HasMorePages = false;
}