Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to print Data Grid using document viewer in c#. The problem is when it has more data than one page. its not creating 2nd page. i think there is some missing or mistake in my code. my code is displaying remaining data but on same page rather than next page. Kindly help me out guys. THANKS in advance.

What I have tried:

Bitmap bmp = Properties.Resources.small_1276_57178cc54660e_png_128x128;
System.Drawing.Image newImage = bmp;
e.Graphics.DrawImage(newImage, 40, 20, newImage.Width, newImage.Height);
//// Header
e.Graphics.DrawString("Asif Traders ", new System.Drawing.Font("Arial", 20, FontStyle.Bold), Brushes.Black, new Point(350, 60));
e.Graphics.DrawString("Address: ", new System.Drawing.Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(280, 95));
e.Graphics.DrawString("Bano Market,Rail Bazar, Arifwala ", new System.Drawing.Font("Arial", 12), Brushes.Black, new Point(300, 95));
e.Graphics.DrawString("Phone #: ", new System.Drawing.Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(310, 115));
e.Graphics.DrawString("0300-8575917 ", new System.Drawing.Font("Arial", 12), Brushes.Black, new Point(360, 115));

e.Graphics.DrawString("Date: ", new System.Drawing.Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(310, 200));
e.Graphics.DrawString(dateTimePicker1.Text, new System.Drawing.Font("Arial", 12), Brushes.Black, new Point(310, 220));
e.Graphics.DrawString("Time: ", new System.Drawing.Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(530, 225));
e.Graphics.DrawString(DateTime.Now.ToString("hh:mm:ss tt", System.Globalization.DateTimeFormatInfo.InvariantInfo),
new System.Drawing.Font("Arial", 12), Brushes.Black, new Point(600, 225));

e.Graphics.DrawString(label9.Text, new System.Drawing.Font("Arial", 12), Brushes.Black, new Point(25, 250));
e.Graphics.DrawString("Account Name", new System.Drawing.Font("Arial", 16), Brushes.Black, new Point(50, 280));
e.Graphics.DrawString("Closing Balance", new System.Drawing.Font("Arial", 16), Brushes.Black, new Point(580, 280));

e.Graphics.DrawString(label9.Text, new System.Drawing.Font("Arial", 12), Brushes.Black, new Point(25, 300));
e.Graphics.DrawLine(new Pen(Color.Black, 1), new Point(28, 267), new Point(28, 858));
e.Graphics.DrawLine(new Pen(Color.Black, 1), new Point(786, 267), new Point(786, 858));
Graphics graphics = e.Graphics;
System.Drawing.Font font = new System.Drawing.Font("Arial", 10);
float fontHeight = font.GetHeight();
int startX = 70;
int startY = 320;
int Offset = 20;

int a = 100;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (i > dataGridView1.Rows[0].Height)
{
dataGridView1.Rows[0].Height = 50;
e.HasMorePages = true;
return;
}
else
{
e.HasMorePages = false;
}

graphics.DrawString(Convert.ToString(dataGridView1.Rows[i].Cells[1].Value), new Font("Courier New", 10), new SolidBrush(Color.Black), startX, startY + Offset);
graphics.DrawString("\t" + Convert.ToString(dataGridView1.Rows[i].Cells[4].Value), new Font("Courier New", 10), new SolidBrush(Color.Black), 280, 350 + Offset);
graphics.DrawString(Convert.ToString(dataGridView1.Rows[i].Cells[2].Value), new Font("Courier New", 10), new SolidBrush(Color.Black), 510, startY + Offset);
graphics.DrawString("\t" + Convert.ToString(dataGridView1.Rows[i].Cells[5].Value), new Font("Courier New", 10), new SolidBrush(Color.Black), 600, 320 + Offset);
e.Graphics.DrawString(label9.Text, new System.Drawing.Font("Arial", 12), Brushes.Black, new Point(25, 340));
Offset = Offset + 25;
e.Graphics.DrawString(label9.Text, new System.Drawing.Font("Arial", 12), Brushes.Black, new Point(25, Offset + startY));

Offset = Offset + 25;


}
e.Graphics.DrawString(label9.Text, new System.Drawing.Font("Arial", 12), Brushes.Black, new Point(25, Offset + startY + 10));
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900