Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have data gridview. there have 7 rows. now i want print all of the row in datagrid view. i tried many ways. according to my code there have only 6 rows print and 1 row missing. how i print all of the rows in my datagridview?

What I have tried:

private void button4_Click(object sender, EventArgs e)
{
printDocument1.Print();
}
private void printDocument1_PrintPage_1(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Bitmap bm = new Bitmap(this.dataGridView1.Width, this.dataGridView1.Height);
dataGridView1.DrawToBitmap(bm, new Rectangle(0, 0, this.dataGridView1.Width, this.dataGridView1.Height));
e.Graphics.DrawImage(bm, 0, 0);
}

my current output is here
dump - Google Drive[^]
Posted
Updated 20-Nov-16 5:24am

1 solution

I tried below code to generate image, its working fine
C#
//Resize DataGridView to full height.
int height = dataGridView1.Height;
dataGridView1.Height = dataGridView1.RowCount * dataGridView1.RowTemplate.Height;

//Create a Bitmap and draw the DataGridView on it.
Bitmap bitmap = new Bitmap(this.dataGridView1.Width, this.dataGridView1.Height);
dataGridView1.DrawToBitmap(bitmap, new Rectangle(0, 0, this.dataGridView1.Width, this.dataGridView1.Height));

//Resize DataGridView back to original height.
dataGridView1.Height = height;

//Save the Bitmap to folder.
bitmap.Save(@"D:\temp\DataGridView.png");
 
Share this answer
 
Comments
Amal anjula 20-Nov-16 11:36am    
there have error
An unhandled exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll

Additional information: A generic error occurred in GDI+.

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