VB.NET - export datagridview to pdf - Asked By Rahul Kumar on 20-Feb-12 01:47 AM

I want to export data from datagridview to the pdf ,i try a lot but unable to find solution code for it so please help me to do this
Web Star replied to Rahul Kumar on 20-Feb-12 01:53 AM
There are lots of third party tool which gives you good feature to export datagridview into pdf
 good one is

dipa ahuja replied to Rahul Kumar on 20-Feb-12 03:26 AM
Download the dll of itextSharp.dll
and try this code
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
private void btnExport_Click(object sender, EventArgs e)
  Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
  PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("d:\\Test.pdf", FileMode.Create));
  doc.Open();//Open Document to write
  Paragraph paragraph = new Paragraph("data Exported From DataGridview!");
  //Create table by setting table value
  Table t1 = new Table(2);
  DataTable dt = (DataTable)dataGridView1.DataSource;
  //Create Table Header
  Cell cid = new Cell("ID");
  Cell cname = new Cell("Name");
  cid.BackgroundColor = iTextSharp.text.Color.GRAY;
  cname.BackgroundColor = iTextSharp.text.Color.GRAY;
  foreach (DataGridViewRow rows in dataGridView1.Rows)
    //check checkbox of datagridview is checked
    if (Convert.ToBoolean(dataGridView1.Rows[rows.Index].Cells[0].Value))
      string id = dataGridView1.Rows[rows.Index].Cells["empid"].Value.ToString();
      string name = dataGridView1.Rows[rows.Index].Cells["ename"].Value.ToString();
      //Create Cells
      Cell c2 = new Cell(id);
      Cell c1 = new Cell(name);
      //Adding cells
  doc.Close(); //Close document
  MessageBox.Show("PDF Created!");
Somesh Yadav replied to Rahul Kumar on 20-Feb-12 03:37 AM
this is the code for export data from gridview to pdf in c#...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.UI.HtmlControls;
using System.Text;

using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;

namespace grid_to_pdf
public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
GridView1.Visible = true;

protected void Button1_Click(object sender, EventArgs e)
Response.ContentType = "application/pdf";
"attachment;filename=GridViewExport.pdf") ;
Response.Cache.SetCacheability(HttpCacheability.No Cache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
Rahul Kumar replied to Somesh Yadav on 20-Feb-12 04:13 AM
this code is for asp.net appklication but i want code for windows application
Rahul Kumar replied to dipa ahuja on 20-Feb-12 06:47 AM
the following dll is not shown in  the add reference category even i add it in the system32 files
dipa ahuja replied to Rahul Kumar on 20-Feb-12 06:51 AM
AS I HAVE ALREADY WRITTEN you have to download the dll


Rahul Kumar replied to dipa ahuja on 20-Feb-12 07:47 AM
i have download the dll and also register it in system32 then also it doesnot show it in add reference option
dipa ahuja replied to Rahul Kumar on 20-Feb-12 09:21 AM
you have to add in your project not in system32

Right click on your project -> Add reference and add the dll by browsing