/// pass gridveiw into this funtion private void ExportGridView(GridView gv) { string attachment = "attachment; filename=ArchiveOrderList.xls"; //name of excel filename Response.ClearContent(); Response.AddHeader("content-disposition", attachment); Response.ContentType = "application/ms-excel"; StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); // Create a form to contain the grid HtmlForm frm = new HtmlForm(); //gvOrderInfo.Parent.Controls.Add(frm); //gvOrderInfo.Parent.Controls.Add(frm); this.Controls.Add(frm); frm.Attributes["runat"] = "server"; frm.Controls.Add(gv); frm.RenderControl(htw); //GridView1.RenderControl(htw); Response.Write(sw.ToString()); Response.End(); } private void PrepareGridViewForExport(Control gv) { LinkButton lb = new LinkButton(); Literal l = new Literal(); string name = String.Empty; for (int i = 0; i < gv.Controls.Count; i++) { if (gv.Controls[i].GetType() == typeof(LinkButton)) { l.Text = (gv.Controls[i] as LinkButton).Text; gv.Controls.Remove(gv.Controls[i]); gv.Controls.AddAt(i, l); } else if (gv.Controls[i].GetType() == typeof(DropDownList)) { l.Text = (gv.Controls[i] as DropDownList).SelectedItem.Text; gv.Controls.Remove(gv.Controls[i]); gv.Controls.AddAt(i, l); } else if (gv.Controls[i].GetType() == typeof(CheckBox)) { l.Text = (gv.Controls[i] as CheckBox).Checked ? "True" : "False"; gv.Controls.Remove(gv.Controls[i]); gv.Controls.AddAt(i, l); } else if (gv.Controls[i].GetType() == typeof(ImageButton)) { continue; } if (gv.Controls[i].HasControls()) { PrepareGridViewForExport(gv.Controls[i]); } } } public override void VerifyRenderingInServerForm(Control control) { if (control.ID == GridView1.Id) // here GridView1 is the id of gridview of page { //Doing nothing return; } base.VerifyRenderingInServerForm(control); }
Total Pageviews
Saturday, April 11, 2009
Export GridView into Excel
below i am describing how to export a grid data..
Subscribe to:
Post Comments (Atom)
Blog Archive
Ideal SQL Query For Handling Error & Transcation in MS SQL
BEGIN TRY BEGIN TRAN --put queries here COMMIT; END TRY BEGIN CATCH IF @@TRANCOUNT>0 BEGIN SELECT @@ERROR,ERRO...
-
All we need to use the Custom paging in grid view without object data source. Here i am going to explain my code which is used in custom pa...
-
One of the common requests in ASP.NET is to submit a form when visitor hits an Enter key. That could be a case if, for example you want to m...
-
using System.Data; using System.Data.SqlClient; using System.Text; using System.IO; using iTextSharp.text; using iTextSharp.text.pdf; ...
No comments:
Post a Comment