Today In this article, I am trying to explain the
concept of datagridview printing in C#.
Implement Print in datagridview
- Open Visual Studio 2010.
- Create a new project and give an appropriate name.
- Drag and drop datagridview in your form from the toolbox.
- Add data in the datagridview.
- Drag and drop the printDocument from the toolbox.
- Add a button for print on the form.
- Double click to get the click event of a button.
- Write the below code in the print button click event.
- printDocument1.Print();
- Double click on the printDocument to get the Print Page event.
- Write the below code for print the datagridview :-
private void printDocument1_PrintPage(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);
}
11. Run the demo project;
Output: -

Note: -
Click on the Print button to get the print of this datagridview.
I am saving the print datagridview in the XPS document.

Leave Comment
6 Comments