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.
Akaganime Max
26-Jan-2019can i reach data in the grid
WHEN I USE PRINT DOCUMENT I CANT REACH DATA
Akaganime Max
26-Jan-2019can i reach data in the grid
WHEN I USE PRINT DOCUMENT I CANT REACH DATA
Mert Emre
02-Mar-2018Dorababu M
04-May-2017Nibaal Bazzi
16-Dec-2013Thanks for sharing.
Before this, I used this C# Excel component and I would first import DataGridView to an Excel file in C#. That created Excel file would contain all the formatting which was made to a DataGridView and after that, I would print an Excel file in C#:
Although the code dimension is larger with your approach, I now have a direct solution which I will certainly try when I do not have to preserve DataGridViews data in some file.
Again thx.
Manmohan Jha
25-Jul-2013