Home > DeveloperSection > Articles > Datagridview Printing in C#

Datagridview Printing in C#

C# C# 
2 Comment(s)
 24097  View(s)
Rate this:

Datagridview Printing in C#

In this article I am trying to explain the concept of datagridview printing in C#.

Implement Print in datagridview

1.      Open Visual Studio 2010.

2.      Create new project and give appropriate name.

3.      Drag and drop datagridview in your form from toolbox.

4.      Add data in the datagridview.

5.      Drag and drop the printDocument from toolbox.

6.      Add a button for print on the form.

7.      Double click to get the click event of button.

8.      Write the below code in print button click event.


9.      Double click on the printDocument to get the Print Page event.

10.  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 Print button to get the print of this datagridview.

I am saving the print datagridview in xps document.

Datagridview Printing in C#

Datagridview Printing in C#

By Manmohan Jha on   3 years ago
Best Code...

PrintDocument pd = new PrintDocument();
public Form1()
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
  private void pd_PrintPage(object sender, PrintPageEventArgs e)
            int LeftMargin = e.MarginBounds.Left;
                bool MorePagesToPrint = false;
                int TmpWidth = 0;
                TopMargin = e.MarginBounds.Top;                

                if (FirstPage)
                    foreach (DataGridViewColumn GridCol in dgvshow.Columns)

                        TmpWidth = (int)(Math.Floor((double)((double)GridCol.Width / (double)TotalWidth * (double)TotalWidth *
                                       ((double)e.MarginBounds.Width / (double)TotalWidth))));

                        HeaderHeight = (int)(e.Graphics.MeasureString(GridCol.HeaderText,
                                    GridCol.InheritedStyle.Font, TmpWidth).Height) + 11;

                        LeftMargin += TmpWidth;
                while (Row < dgvshow.Rows.Count)
                    DataGridViewRow GridRow = dgvshow.Rows[Row];
                    CellHeight = GridRow.Height + 5;
                    int Count = 0;
                    //Check whether the current page settings allo more rows to print
                    if (TopMargin + CellHeight >= e.MarginBounds.Height + e.MarginBounds.Top)
                        NewPage = true;
                        FirstPage = false;
                        MorePagesToPrint = true;
                        if (NewPage)

                            foreach (DataGridViewColumn GridCol in dgvshow.Columns)
                                e.Graphics.FillRectangle(new SolidBrush(Color.LightGray),
                                    new Rectangle((int)AColumnLefts[Count], TopMargin,
                                    (int)AColumnWidths[Count], HeaderHeight));

                                    new Rectangle((int)AColumnLefts[Count], TopMargin, (int)AColumnWidths[Count], HeaderHeight));

                                e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font,
                                    new SolidBrush(GridCol.InheritedStyle.ForeColor),
                                    new RectangleF((int)AColumnLefts[Count], TopMargin,
                                    (int)AColumnWidths[Count], HeaderHeight), strFormat);
                            NewPage = false;
                            TopMargin += HeaderHeight;
                        Count = 0;
                        //Draw Columns Contents   

                        foreach (DataGridViewCell Cel in GridRow.Cells)
                            if (Cel.Value != null)
                                e.Graphics.DrawString(Cel.Value.ToString(), Cel.InheritedStyle.Font,
                                            new SolidBrush(Cel.InheritedStyle.ForeColor),
                                            new RectangleF((int)AColumnLefts[Count], (float)TopMargin,
                                            (int)AColumnWidths[Count], (float)CellHeight), strFormat);
                            //Drawing Cells Borders 
                            e.Graphics.DrawRectangle(Pens.Black, new Rectangle((int)AColumnLefts[Count], TopMargin, (int)AColumnWidths[Count], CellHeight));

                    TopMargin += CellHeight;

                //If more lines exist, print another page.
                if (MorePagesToPrint)
                    e.HasMorePages = true;
                    e.HasMorePages = false;           

private void btnprint_Click(object sender, EventArgs e)
    PrintDialog printDialog = new PrintDialog();
    printDialog.Document = pd;
    printDialog.UseEXDialog = true;

      if (DialogResult.OK == printDialog.ShowDialog())
      pd.DocumentName = "Page Title";


Datagridview Printing in C#

By Nibaal Bazzi on   2 years ago

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

var file = new ExcelFile();
var sheet = file.Worksheets.Add("DataGridView");

new ImportFromDataGridViewOptions() { ColumnHeaders = true });


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.

Don't want to miss updates? Please click the below button!

Follow MindStick