0

enter image description here

Trying to Export the Data Grid View to a PDF file using C#

The btnexport_Click event calls the Method ExportGridToPDF();

Here is the code below:

   private void btnexport_Click(object sender, EventArgs e)

    {          
      ExportGridToPDF();  
    }


   private void ExportGridToPDF()

    {
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=GetStudent.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        dataGridView1.RenderControl(hw);
        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);
        pdfDoc.Open();
        htmlparser.Parse(sr);
        pdfDoc.Close();
        Response.Write(pdfDoc);
        Response.End();
        dataGridView1.AllowPaging = false;
        dataGridView1.DataBind();           
    } 
pkc456
  • 8,350
  • 38
  • 53
  • 109
  • 1
    Add the problem/error details you are facing, so that someone here can help you with it. – Devraj Gadhavi May 05 '16 at 06:02
  • As mentioned by @DevrajGadhavi, please share the error details. Also try to look into iTextSharp as this might be handy for your requirement. could be useful http://stackoverflow.com/questions/15040869/how-i-get-data-of-datagridview-in-pdf-in-c-sharp – LifeOfPi May 05 '16 at 06:54
  • This is a web project? – llouk May 05 '16 at 09:46

0 Answers0