Export to Excel Removing the DropDownList and LinkButtons from the Exported File

If you have DropDownLists and LinkButtons inside the GridView control and you try to export the GridView to excel then the controls contained in the GridView are also exported to the excel spreadsheet. You can remove those controls and replace them with their selected value. Here is a small method called DisableControls that takes Control as parameter and replaces the DropDownLists and LinkButtons with Literal control.

private  void DisableControls(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 
as LinkButton).Text;
gv.Controls.Remove(gv.Controls);
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);

}

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

You can also view the complete article at this link: http://gridviewguy.com/ArticleDetails.aspx?articleID=197

powered by IMHO 1.3

Print | posted @ Sunday, August 13, 2006 11:28 PM

Twitter