Reputation: 65
I'm trying to export a gridview to pdf with package iTextSharp.
I'm doing it in my .aspx files :
<form id="formOptions" runat="server">
[...]
<asp:GridView ID="gvReportingStockComp" runat="server" AutoGenerateColumns="false" Visible="true">
<Columns>
<asp:BoundField DataField="cod_wo" HeaderText="N° OF" />
<asp:BoundField DataField="composant" HeaderText="Composant" />
<asp:BoundField DataField="BESOIN" HeaderText="Besoin/OF" />
<asp:BoundField DataField="BESOIN_T" HeaderText="Besoin total" />
<asp:BoundField DataField="stock_dispo" HeaderText="Stock dispo" />
<asp:BoundField DataField="QTE_RESTANTE" HeaderText="Qte restante" />
</Columns>
</asp:GridView>
</form>
And in my code behind, I fill the gridview :
OracleConnection oConnexion = new OracleConnection();
oConnexion.ConnectionString = "X";
oConnexion.Open();
string reqStockCompTotal = "intitulé de ma requete"
OracleCommand cmdReqStockComp = new OracleCommand(reqStockCompTotal);
cmdReqStockComp.Connection = oConnexion;
OracleDataReader readerReqStockComp = cmdReqStockComp.ExecuteReader();
gvReportingStockComp.DataSource = readerReqStockComp;
gvReportingStockComp.DataBind();
oConnexion.Close();
oConnexion.Dispose();
It work buit if I add the next code in order to make the export :
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=TestMES.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gvReportingStockComp.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();
In a first hand it block with :
gvReportingStockComp.RenderControl(hw);
and visual studio says : "GridView must be declared in with runat=server"
Or it block to :
pdfDoc.Close();
and he says : pdfDoc is empty...
Somebody has an idea please ?
Upvotes: 0
Views: 1990
Reputation: 2895
Add below code to avoid runat="server" error.
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
You may want to disable paging so that it exports all rows of Gridview to PDF.
Add below code before gridview's rendercontrol method.
gvReportingStockComp.AllowPaging = false;
gvReportingStockComp.DataBind();
Incase you've any doubt, refer below article.
http://www.aspsnippets.com/Articles/Export-GridView-To-Word-Excel-PDF-CSV-Formats-in-ASP.Net.aspx
Upvotes: 1
Reputation: 3256
Add the Rendering
Function after your PDF code
public override void VerifyRenderingInServerForm(Control control)
{
// verifies the control is rendered here
}
Upvotes: 0