I have done printing of datagridview in c# .
it works fine. But when gridview as lot of records in print preview it displays all values. I need datagrid view with a finite set of rows per page (say 10). how to print gridview in multiple pages, 10 rows per page.
private void btnPrint_Click(object sender, EventArgs e)
{
int height = dgvP.Height;
dgvP.Height = .dgvP.RowCount * dgvP.RowTemplate.Height;
bitmap = new Bitmap(this.dgvP.Width, this.dgvP.Height);
dgvP.DrawToBitmap(bitmap, new Rectangle(0, 0, this.dgvP.Width, this.dgvP.Height));
//Resize DataGridView back to original height.
dgvP.Height = height;
//Show the Print Preview Dialog.
printPreviewDialog1.Document = printDocument1;
//printPreviewDialog1.PrintPreviewControl.Zoom = 1;
printPreviewDialog1.ShowDialog();
}
private void printDocument1_PrintPage_1(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
PrintDocument pd = new PrintDocument();
pd.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("PaperA4", 940, 1180);
int y = 225;
int width = 0;
int height = 0;
StringFormat str = new StringFormat();
str.Alignment = StringAlignment.Near;
str.LineAlignment = StringAlignment.Center;
str.Trimming = StringTrimming.EllipsisCharacter;
Pen p = new Pen(Color.Black, 2.5f);
#region Draw Column 1
e.Graphics.FillRectangle(Brushes.LightGray, new Rectangle(160, 200, 150, dgvPatientSearch.Rows[0].Height));
e.Graphics.DrawRectangle(Pens.Black, 160, 200, 150, dgvP.Rows[0].Height);
e.Graphics.DrawString(dgvP.Columns[0].HeaderText, dgvP.Font, Brushes.Black, new RectangleF(160, 200, 150, dgvP.Rows[0].Height), str);
width = 160 + 150;
height = 100;
#endregion
#region Draw column 2
e.Graphics.FillRectangle(Brushes.LightGray, new Rectangle(310, 200, 90, dgvP.Rows[0].Height));
e.Graphics.DrawRectangle(Pens.Black, 310, 200, 90, dgvP.Rows[0].Height);
e.Graphics.DrawString(dgvPatientSearch.Columns[1].HeaderText, dgvPatientSearch.Font, Brushes.Black, new RectangleF(310, 200, 90, dgvPatientSearch.Rows[0].Height), str);
width = 310 + 90;
height = 100;
#endregion
#region Draw column 3
e.Graphics.FillRectangle(Brushes.LightGray, new Rectangle(400, 200, 90, dgvP.Rows[0].Height));
e.Graphics.DrawRectangle(Pens.Black, 400, 200, 90, dgvP.Rows[0].Height);
e.Graphics.DrawString(dgvP.Columns[2].HeaderText, dgvP.Font, Brushes.Black, new RectangleF(400, 200, 90, dgvP.Rows[0].Height), str);
width = 400 + 90;
height = 100;
#endregion
#region Draw column 4
e.Graphics.FillRectangle(Brushes.LightGray, new Rectangle(490, 200, 90, dgvP.Rows[0].Height));
e.Graphics.DrawRectangle(Pens.Black, 490, 200, 90, dgvP.Rows[0].Height);
e.Graphics.DrawString(dgvP.Columns[3].HeaderText, dgvP.Font, Brushes.Black, new RectangleF(490, 200, 90, dgvP.Rows[0].Height), str);
width = 490 + 90;
height = 100;
#endregion
#region Draw column 5
e.Graphics.FillRectangle(Brushes.LightGray, new Rectangle(580, 200, 90, dgvPatientSearch.Rows[0].Height));
e.Graphics.DrawRectangle(Pens.Black, 580, 200, 90, dgvP.Rows[0].Height);
e.Graphics.DrawString(dgvP.Columns[4].HeaderText, dgvP.Font, Brushes.Black, new RectangleF(580, 200, 90, dgvP.Rows[0].Height), str);
width = 580 + 90;
height = 100;
#endregion
int i=0;
while (i < dgvP.Rows.Count - 1)
{
if (height > e.MarginBounds.Height)
{
height = 100;
width = 100;
e.HasMorePages = true;
return;
}
height += dgvP.Rows[i].Height;
e.Graphics.DrawRectangle(Pens.Black, 160, y, 150, dgvP.Rows[i].Height);
e.Graphics.DrawString(dgvP.Rows[i].Cells[0].FormattedValue.ToString(), dgvP.Font, Brushes.Black, new RectangleF(160, y, 150, dgvP.Rows[i].Height), str);
height = 100;
e.Graphics.DrawRectangle(Pens.Black, 310, y, 90, dgvP.Rows[i].Height);
e.Graphics.DrawString(dgvP.Rows[i].Cells[1].FormattedValue.ToString(), dgvP.Font, Brushes.Black, new RectangleF(310, y, 90, dgvP.Rows[i].Height), str);
e.Graphics.DrawRectangle(Pens.Black, 400, y, 90, dgvP.Rows[i].Height);
e.Graphics.DrawString(dgvP.Rows[i].Cells[2].FormattedValue.ToString(), dgvP.Font, Brushes.Black, new RectangleF(400, y, dgvP.Columns[0].Width, dgvP.Rows[i].Height), str);
e.Graphics.DrawRectangle(Pens.Black, 490, y, 90, dgvP.Rows[i].Height);
e.Graphics.DrawString(dgvPatientSearch.Rows[i].Cells[3].FormattedValue.ToString(), dgvPatientSearch.Font, Brushes.Black, new RectangleF(490, y, dgvP.Columns[0].Width, dgvP.Rows[i].Height), str);
e.Graphics.DrawRectangle(Pens.Black, 580, y, 90, dgvP.Rows[i].Height);
e.Graphics.DrawString(dgvP.Rows[i].Cells[4].FormattedValue.ToString(), dgvP.Font, Brushes.Black, new RectangleF(580, y, dgvP.Columns[0].Width, dgvP.Rows[i].Height), str);
i++;
y = y + 25;
}
}