Click here to Skip to main content
15,888,297 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have this code for downloading the gridview into a pdf file. My Problem is that it only Downloads the column that are visible. The other columns have values too, they are just not visible.

My Question is: how to show all the columns in the gridview with data and hide the columns that are empty.

C#
public override void VerifyRenderingInServerForm(Control control)
{

}

protected void btnGenerate_Click(object sender, EventArgs e)
{

    string FilePath = MapPath("~/File/doc.pdf");


    iTextSharp.text.Document pdfDoc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 20f, 20f, 20f, 20f);
    PdfWriter.GetInstance(pdfDoc, new FileStream(FilePath, FileMode.Create));
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);

    GridView1.AllowPaging = false;
    GridView1.HeaderRow.Cells[1].Text = "Titel";
    GridView1.HeaderRow.Font.Bold = true;
    GridView1.RenderControl(hw);

    StringReader sr = new StringReader(sw.ToString());
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();

    Response.Write(pdfDoc);

    Response.ContentType = "Application/pdf";
    Response.WriteFile(FilePath);
    Response.End();
  }


What I have tried:

Unfortunately nothing came to my mind
Posted
Updated 25-Oct-16 2:45am
v2

1 solution

You can rebind gridview inside btngenerate click
if you have third column visible =false from design, enable it from btngenerate click and rebind your gridview
C#
DataTable dtSource = GetdataSource();
GridView1.DataSource = dtSource;
GridView1.Columns[2].Visible = true;
GridView1.DataBind();
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900