Nesreen adly
Nesreen adly

Reputation: 55

Import Gridview to pdf

I need to Import data from Grid view in asp.net to pdf file this is my Gridview

<asp:GridView ID="gvValues" runat="server" Width="100%" AllowPaging="True" PagerSettings-Mode="NumericFirstLast"
    AutoGenerateColumns="False" CellPadding="0" PageSize="15" ItemType="Tayf.Models.Product" CssClass="table-striped table-condensed table table-bordered table-hover"
    OnRowDataBound="gvValues_RowDataBound" OnPageIndexChanging="gvValues_PageIndexChanging" meta:resourcekey="gvValuesResource1" EmptyDataText="No Products in your Pos">
        <EmptyDataRowStyle Font-Bold="True" Font-Size="16pt" ForeColor="Red" />
           <RowStyle Wrap="true" />
               <Columns>
                    <asp:TemplateField HeaderText="#">
                       <ItemTemplate><%# gvValues.PageSize*gvValues.PageIndex+ Container.DisplayIndex+1  %></ItemTemplate>
                     </asp:TemplateField>
                      <asp:BoundField DataField="Barcode" ReadOnly="true" HeaderText="Barcode" meta:resourcekey="BoundFieldResource1" />
                     <asp:BoundField DataField="Description" ReadOnly="true" HeaderText="Item Name" meta:resourcekey="BoundFieldResource2" />
                      <asp:BoundField DataField="Price" DataFormatString="{0:#.00}" ReadOnly="true" HeaderText="Price" meta:resourcekey="BoundFieldResource3" />
                      <asp:TemplateField HeaderText="Notes">
                      <ItemTemplate><%# Item==null?"":  Item.PosInvoiceDetail == null || Item.PosInvoiceDetail.Count<1?"": "Invoice ID:" +  Item.PosInvoiceDetail.First().PosInvoiceMasterID.ToString()  %></ItemTemplate>
                      </asp:TemplateField>
                </Columns>

</asp:GridView>

and this button

<asp:Button  ID="importData" runat="server" CssClass="btn-default" Text="Import To PDF" OnClick="importData_Click"></asp:Button>

and this is my Code

 protected void importData_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);
            gvValues.AllowPaging = false;
            gvValues.DataBind();
            gvValues.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(); 
        }

But I have an Error

which said

An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user code

Additional information: Control 'MainContent_gvValues' of type 'GridView' must be placed inside a form tag with runat=server.

Upvotes: 0

Views: 230

Answers (1)

Dipak Panchal
Dipak Panchal

Reputation: 36

  • This exception occurs when one tries to export a GridView control to Word, Excel, PDF, CSV or any other formats. Here the .net compiler thinks that the control is not added to the form and is rendered

Solution of this Problem is Just write below code in CS. File

   public override void VerifyRenderingInServerForm(Control control)
    {
        /* Confirms that an HtmlForm control is rendered for the specified AS.net
           server control at run time. */

    }

Upvotes: 1

Related Questions