articles

Home / DeveloperSection / Articles / Datagridview Printing in C#

Datagridview Printing in C#

Datagridview Printing in C#

Vijay Shukla 57040 23-Jun-2013

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: -

Datagridview Printing in C#

Note: -

Click on the Print button to get the print of this datagridview.

I am saving the print datagridview in the XPS document.

Datagridview Printing in C#


c# c# 
Updated 30-Jan-2020

Leave Comment

Comments

Liked By