---
title: "Add datagridview printing in C#"  
description: "Add datagridview printing in C#"  
author: "Mark Devid"  
published: 2013-06-15  
updated: 2013-07-24  
canonical: https://www.mindstick.com/forum/1158/add-datagridview-printing-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Add datagridview printing in C#

HI Experts,

How can I [add](https://www.mindstick.com/forum/12983/add-address-in-textbox-when-page-is-load-and-if-i-search-address-in-textbox-show-in-map-by-javascript) [printing](https://www.mindstick.com/blog/63804/5-ways-you-can-use-printing-in-your-marketing-strategy) [option](https://www.mindstick.com/forum/159391/jquery-get-selected-option-from-dropdown) in a [datagridview](https://www.mindstick.com/articles/607/working-with-datagridview-in-vc-sharp) using C#.

## Replies

### Reply by Manmohan Jha

Hello Mark,\
You can use this 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;\
AColumnLefts.Add(LeftMargin); AColumnWidths.Add(TmpWidth); LeftMargin += TmpWidth; } } //MessageBox.Show(dgvshow.Rows.Count.ToString()); while (Row < dgvshow.Rows.Count) { //MessageBox.Show("Ok"); 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; break; } else { 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));\
e.Graphics.DrawRectangle(Pens.Black, 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); Count++; } 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));\
Count++; } } Row++; TopMargin += CellHeight; }\
//If more lines exist, print another page. if (MorePagesToPrint) e.HasMorePages = true; else 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"; pd.Print(); }}\
Mj

### Reply by Sumit Kesarwani

Hi Mark,\
You can use this code:\

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);

}

private void button1_Click(object sender, EventArgs e)

{

printDocument1.Print();

}


---

Original Source: https://www.mindstick.com/forum/1158/add-datagridview-printing-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
