selva kumar

selva kumar

  • NA
  • 557
  • 189.8k

Problem While exporting gridview data to pdf?

Jan 31 2014 2:56 AM
while exporting gridview data to pdf.Header text does not displayed in pdf. Data's only displayed.can anyone help me out...
here is my code....

Html:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
                        ForeColor="#333333" GridLines="None" Width="773px">
                        <RowStyle BackColor="#E3EAEB" />
                        <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
                        <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
                        <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                        <EditRowStyle BackColor="#7C6F57" />
                        <AlternatingRowStyle BackColor="White" />
                       
                        <Columns>
                        <asp:TemplateField>
                        <ItemTemplate>
                        <asp:Label ID="lbllid" runat="server" Visible="false" Text='<%#Eval("T_ID") %>'></asp:Label>
                        </ItemTemplate>
                        </asp:TemplateField>
                       
                        <asp:TemplateField HeaderText="Serial No">
                        <ItemTemplate>
                        <%#((GridViewRow)Container).RowIndex+1 %>
                        </ItemTemplate>
                        </asp:TemplateField>
                       
                        <asp:TemplateField HeaderText="Branch">
                        <ItemTemplate>
                        <asp:Label ID="iblbranchname" runat="server" Text='<%#Eval("BranchName") %>'></asp:Label>
                        </ItemTemplate>
                        </asp:TemplateField>
                       
                        <asp:TemplateField HeaderText="Type">
                        <ItemTemplate>
                        <asp:Label ID="ibltype" runat="server" Text='<%#Eval("Type") %>'></asp:Label>
                        </ItemTemplate>
                        </asp:TemplateField>
                       
                        <asp:TemplateField HeaderText="Brand">
                        <ItemTemplate>
                        <asp:Label ID="iblbrand" runat="server" Text='<%#Eval("Brand") %>'></asp:Label>
                        </ItemTemplate>
                        </asp:TemplateField>
                       
                        <asp:TemplateField HeaderText="Received Date">
                        <ItemTemplate>
                        <asp:Label ID="iblreceiveddate" runat="server" Text='<%#Eval("ReceivedDate") %>'></asp:Label>
                        </ItemTemplate>
                        </asp:TemplateField>
                       
                        <asp:TemplateField HeaderText="Stock Received">
                        <ItemTemplate>
                        <asp:Label ID="iblstockreceived" runat="server" Text='<%#Eval("ReceivedStock") %>'></asp:Label>
                        </ItemTemplate>
                        </asp:TemplateField>
                       
                        <asp:TemplateField HeaderText="Stock On Hand">
                        <ItemTemplate>
                        <asp:Label ID="iblstockonhand" runat="server" Text='<%#Eval("StockOnHand") %>'></asp:Label>
                        </ItemTemplate>
                        </asp:TemplateField>
                       
                        <asp:TemplateField HeaderText="Sold Date">
                        <ItemTemplate>
                        <asp:Label ID="iblsolddate" runat="server" Text='<%#Eval("SoldDate") %>'></asp:Label>
                        </ItemTemplate>
                        </asp:TemplateField>
                       
                        <asp:TemplateField HeaderText="Quantity">
                        <ItemTemplate>
                        <asp:Label ID="iblquantity" runat="server" Text='<%#Eval("Quantity") %>'></asp:Label>
                        </ItemTemplate>
                        </asp:TemplateField>
                       
                        </Columns>
                    </asp:GridView>


C#;

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using System.Net;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;

public partial class Admin_SalesReport : System.Web.UI.Page
{
    SqlConnection Con;
    SqlCommand Cmd;
    SqlDataAdapter Sdap;
    protected void Page_Load(object sender, EventArgs e)
    {
        Con = new SqlConnection(@"Data Source=SMAART-1F1D3825\SQLEXPRESS;Initial Catalog=SundarTyres;Integrated Security=True");
        if (!IsPostBack)
        {
            ddl1bind();
            DropDownList2.Items.Add("<--Select-->");
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        gvbind();
        btnprint.Visible = true;
        Btndownload.Visible = true;
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        /* Verifies that the control is rendered */
    }
    protected void Btndownload_Click(object sender, EventArgs e)
    {
        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter hw = new HtmlTextWriter(sw))
            {
                //To Export all pages
                GridView1.AllowPaging = false;
                this.gvbind();

                GridView1.RenderControl(hw);
                StringReader sr = new StringReader(sw.ToString());
                Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f);
                HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
                PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
                pdfDoc.Open();
                htmlparser.Parse(sr);
                pdfDoc.Close();

                Response.ContentType = "application/pdf";
                Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.Write(pdfDoc);
                Response.End();
            }
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList2.Items.Clear();
        ddl2bind();
    }
    public void ddl1bind()
    {
        try
        {
            Con.Open();
            Cmd = new SqlCommand("Select Distinct Branch From User_Roles", Con);
            SqlDataReader dr;
            dr = Cmd.ExecuteReader();
            DropDownList1.Items.Add("<--Select-->");
            while (dr.Read())
            {
                DropDownList1.Items.Add(dr["Branch"].ToString());
            }
        }
        catch
        {
        }
        finally
        {
            Con.Close();
        }
    }
    public void ddl2bind()
    {
        try
        {
            Con.Open();
            Cmd = new SqlCommand("Select SoldDate From StockManagement Where BranchName='" + DropDownList1.SelectedItem.Text + "'", Con);
            SqlDataReader dr;
            dr = Cmd.ExecuteReader();
            DropDownList2.Items.Add("<--Select-->");
            while (dr.Read())
            {
                DropDownList2.Items.Add(dr["SoldDate"].ToString());
            }
        }
        catch
        {
        }
        finally
        {
            Con.Close();
        }
    }
    public void gvbind()
    {
        try
        {
            Con.Open();
            Sdap = new SqlDataAdapter("Select * From StockManagement Where BranchName='"+DropDownList1.SelectedItem.Text+"' and SoldDate='"+DropDownList2.SelectedItem.Text+"' ", Con);
            DataTable Ds = new DataTable();
            Sdap.Fill(Ds);
            GridView1.DataSource = Ds;
            GridView1.DataBind();
        }
        catch
        {
        }
        finally
        {
            Con.Close();
        }
    }
}