converting gdridview having templatef field to pdf

Swappy_Gaj
Posted by Swappy_Gaj under ASP.NET category on | Points: 40 | Views : 943
Code for converting Gridview having templatefield to pdf documant;
For Queries(email:swapnilgajbhiye12@gmail.com)
Requirements:
1)iTextSharp.dll(can be downloaded from sourceforge.net)
2)gridview with template field like dropdown,textbox,etc.
3)datatable
4)button( for generating pdf)

How to do it:
First convert gridview to datatable and then datatable to pdf using iTextSharp.dll
1)converting gridview to pdf
code:
dt.Columns.Add(new System.Data.DataColumn("EXAM TIMETABLE", typeof(String)));
//dt.Columns.Add(new System.Data.DataColumn("BookQty", typeof(String)));
//dt.Columns.Add(new System.Data.DataColumn("BookImg", typeof(String)));
//GridViewRow row1 = GridView1.HeaderRow;


foreach (GridViewRow row in GridView1.Rows)
{

//Image Bookimg = (Image)row.FindControl("BookImg");
//Label Booknames = (Label)row.FindControl("lblBookName");
//TextBox Bookqty = (TextBox)row.FindControl("TXTQty");
//Label TotalPrice = (Label)row.FindControl("LBLTotal");
//DropDownList d = row.FindControl("DropDownList1") as DropDownList;
Label l2 = (Label)row.FindControl("Label2");
DropDownList d1 = GridView1.HeaderRow.FindControl("DropDownList1") as DropDownList;
DropDownList d2= GridView1.HeaderRow.FindControl("DropDownList2") as DropDownList;
DropDownList d3 = (DropDownList)row.FindControl("DropDownList3");
TextBox t1 = (TextBox)row.FindControl("TextBox1");


DropDownList d4 = GridView1.HeaderRow.FindControl("DropDownList4") as DropDownList;
DropDownList d5 = GridView1.HeaderRow.FindControl("DropDownList5") as DropDownList;
DropDownList d6 = (DropDownList)row.FindControl("DropDownList6");
TextBox t2 = (TextBox)row.FindControl("TextBox2");


DropDownList d9 = GridView1.HeaderRow.FindControl("DropDownList9") as DropDownList;
DropDownList d10 = GridView1.HeaderRow.FindControl("DropDownList10") as DropDownList;
DropDownList d11 = (DropDownList)row.FindControl("DropDownList11");
TextBox t3 = (TextBox)row.FindControl("TextBox3");

dr = dt.NewRow();
// dr[0] = dh.Text + "-" + dh2.Text + ":" + dh3.Text;
dr[0] = l2.Text + d1.SelectedValue+"-"+ d2.SelectedValue +"["+ d3.SelectedValue +"]"+ t1.Text+"|"+d4.SelectedValue+"-"+d5.SelectedValue+"["+d6.SelectedValue+"]"+t2.Text+"|"+d9.SelectedValue+"-"+d10.SelectedValue+"["+d11.SelectedValue+"]"+t3.Text;




// dr[0] = dh.Text;
//dr[0] = d.Text;
//dr[1] = Bookqty.Text;
//dr[2] = Bookimg.ImageUrl.ToString();
dt.Rows.Add(dr);
}
// Session["QtyTable"] = dt;
GridView2.DataSource = dt;
GridView2.DataBind();

2)click on generate pdf button
code:
Response.ContentType = "application/pdf";

Response.AddHeader("content-disposition", "attachment;filename=Export.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
HtmlForm frm = new HtmlForm();
GridView2.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(GridView2);
frm.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
iTextSharp.text.Document pdfDoc = new iTextSharp.text.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();

Comments or Responses

Login to post response