Remove link buttons in grid while exporting to excel

Leave a Comment


Let us say suppose we have link buttons and some other dropdown are in the gridview data,when we try to export the grid it will come as it is .

protected void btnExportToExcel_Click(object sender, EventArgs e)
        {
            try
            {
                //calling a method in BAL that will replace the Linkbuttons controls to normal controls
                objBAL.PrepareGridViewForExport(GridView1);
                ExportToExcel();
            }
         Catch(exception ex)
        {
          //Exception handling
        }
  }

//Method in BAL Class
public 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);
                }

                if (gv.Controls[i].HasControls())
                {
                    PrepareGridViewForExport(gv.Controls[i]);
                }
            }
        }


Related Post

0 comments:

Post a Comment