Following works for me
protected void Page_Load(object sender, EventArgs e)
{
SqlDataSource SqlDataSource1 = new SqlDataSource();
SqlDataSource1.ID = "SqlDataSource1";
Page.Controls.Add(SqlDataSource1);
SqlDataSource1.ConnectionString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
SqlDataSource1.SelectCommand = "SELECT * from Customers";
SqlDataSource1.Select(DataSourceSelectArguments.Empty);
GridView1.DataSource = SqlDataSource1;
GridView1.DataBind();
}
protected void btnExportPDF_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.DataBind();
GridView1.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();
}