Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All
I Want to export the gridview data to excel for that i have done this code is working properly without updatepanel

C#
public override void VerifyRenderingInServerForm(Control control)
       {

       }

try
           {

               Response.Buffer = true;
               Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.xls"));
               Response.ContentType = "application/ms-excel";
               StringWriter sw = new StringWriter();
               HtmlTextWriter htw = new HtmlTextWriter(sw);
               gv_forms.AllowPaging = false;
               SearchBlank();
               //gv_forms.DataBind();
               //Change the Header Row back to white color
               gv_forms.HeaderRow.Style.Add("background-color", "#FFFFFF");
               //Applying stlye to gridview header cells
               for (int i = 0; i < gv_forms.HeaderRow.Cells.Count; i++)
               {
                   gv_forms.HeaderRow.Cells[i].Style.Add("background-color", "#507CD1");
               }
               int j = 1;
               //This loop is used to apply stlye to cells based on particular row
               foreach (GridViewRow gvrow in gv_forms.Rows)
               {
                   gvrow.BackColor = Color.White;
                   if (j <= gv_forms.Rows.Count)
                   {
                       if (j % 2 != 0)
                       {
                           for (int k = 0; k < gvrow.Cells.Count; k++)
                           {
                               gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
                           }
                       }
                   }
                   j++;
               }
               CheckBox ItemAll = (CheckBox)Convert.ChangeType((gv_forms.HeaderRow.FindControl("chk_all")), typeof(CheckBox));
               ItemAll.Visible = false;
               foreach (GridViewRow row in gv_forms.Rows)
               {
                   LinkButton lbtnview = (LinkButton)row.FindControl("lbtn_Edit");
                   LinkButton lbtnedit = (LinkButton)row.FindControl("LinkButton2");
                   CheckBox Item = (CheckBox)row.FindControl("chk_item");

                   lbtnview.Visible = false;
                   lbtnedit.Visible = false;
                   Item.Visible = false;

                   gv_forms.Columns[0].Visible = false;
                   gv_forms.Columns[5].Visible = false;
                   gv_forms.Columns[6].Visible = false;

               }
               gv_forms.RenderControl(htw);
               Response.Write(sw.ToString());
               Response.End();
           }
           catch { }

this code is working properly without update panel.
bet when we use update panel(Grid view inside the update panel)then it is not working plz help me
Thanks In Advance.
Posted
Updated 7-Apr-12 1:24am
v3

You can use this custom control
Export to Excel
 
Share this answer
 
By using Post back Triggers with Control id to Button to export
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900