here is my code....
Html:
<%#((GridViewRow)Container).RowIndex+1 %>
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();
}
}
}

Replies
Know the answer? Post it — somebody with the same question will find it here.