DatagridView Print Priview
I want to Print Preview Datagridview, I get this Message Document does not contain any Pages
here is my code
private void printDocument1_PrintPage(object sender,System.Drawing.Printing.PrintPageEventArgs e)
{
System.Drawing.Font b_font = new System.Drawing.Font(this.Font, FontStyle.Bold);
Graphics g = e.Graphics;
DrawForm(e);
DrawDataGridView(b_font, g);
}
private void DrawForm(System.Drawing.Printing.PrintPageEventArgs e)
{
Bitmap bmp=new Bitmap(impdg.Width,impdg.Height);
impdg.DrawToBitmap(bmp, new System.Drawing.Rectangle(0, 0, impdg.Width, impdg.Height));
e.Graphics.DrawImage(bmp, new System.Drawing.Point(0, 0));
}
private void DrawDataGridView(System.Drawing.Font b_font, Graphics g)
{
int columnpos = 0;
int rowpos = 25;
DrawHeaders(b_font, g, ref columnpos, ref rowpos);
rowpos += 65;
DrawGridBody(g, ref columnpos, ref rowpos);
}
private void DrawGridBody(Graphics g, ref int columnpos, ref int rowpos)
{
foreach (DataRow dr in ds.Tables[0].Rows)
{
columnpos = 0;
g.DrawLine(Pens.Black, new System.Drawing.Point(0, rowpos), new System.Drawing.Point(this.Width, rowpos));
foreach (DataGridViewColumn dc in impdg.Columns)
{
string text = dr[dc.DataPropertyName].ToString();
if (dc.DefaultCellStyle.Font != null)
g.DrawString(text, dc.DefaultCellStyle.Font, Brushes.Black, (float)columnpos, (float)rowpos + 20f);
else
g.DrawString(text, this.Font, Brushes.Black, (float)columnpos, (float)rowpos + 20f);
columnpos += dc.Width + 5;
}
rowpos = rowpos + 65;
}
}
private int DrawHeaders(System.Drawing.Font b_font, Graphics g, ref int columnpos, ref int rowpos)
{
foreach (DataGridViewColumn dc in impdg.Columns)
{
g.DrawString(dc.HeaderText,b_font,Brushes.Black,(float)columnpos,(float)rowpos);
columnpos+=dc.Width+5;
}
return columnpos;
}
private void printPreview_Click(object sender, EventArgs e)
{
prntpriew = new PrintPreviewDialog();
if (prntpriew.ShowDialog() == DialogResult.OK)
{
prntpriew.ShowDialog();
}
}