如何在itextsharp中显示所有gridview列?
问题描述:
我有这个代码用于将gridview下载到pdf文件中。我的问题是它只下载可见的列。其他列也有值,它们只是不可见。
我的问题是:如何用数据显示gridview中的所有列并隐藏空列。
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.
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
答
你可以在btngenerate中重新绑定gridview点击
如果你从设计中得到第三列visible = false,从btngenerate单击并重新绑定你的gridview启用它
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
DataTable dtSource = GetdataSource();
GridView1.DataSource = dtSource;
GridView1.Columns[2].Visible = true;
GridView1.DataBind();