I use a lot of hyperlink columns in my EntityGridView controls. This is a problem when users export the grid to excel. I have modified my local copy of the ExportToExcel class to fix this, so I thought I would publish it so that someone with more experience with the template modification could possibly implement it. I hope this helps someone else out as well.
/// <summary>
/// Clears the child controls of a EntityGridView to make sure all controls are LiteralControls
/// </summary>
/// <param name="dg">Datagrid to be cleared and verified</param>
protected void ClearChildControls(System.Web.UI.WebControls.GridView dg)
{
for (int i = dg.Columns.Count - 1; i >= 0; i--)
{
if (dg.Columns.GetType().Name == "ButtonColumn"
|| dg.Columns.GetType().Name == "CheckBoxField"
|| dg.Columns.GetType().Name == "CommandField")
{
dg.Columns.Visible = false;
}
}
// Remove hyperlinks from the grid
foreach (GridViewRow row in dg.Rows)
{
foreach (TableCell cell in row.Cells)
{
if (cell.Text == "")
{
if (cell.Controls.Count > 0)
{
HyperLink lnk = (HyperLink)cell.Controls[0];
if (lnk != null)
{
cell.Text = lnk.Text;
cell.Controls.Clear();
}
}
}
}
}
this.RecursiveClear(dg);
}
}