CodeSmith Community
Your Code. Your Way. Faster!

Export to Excel - Hyperlink columns

Latest post 05-02-2007 1:24 PM by basolutions. 0 replies.
  • 05-02-2007 1:24 PM

    • basolutions
    • Top 100 Contributor
    • Joined on 03-21-2007
    • Denver, CO
    • Posts 51
    • Points 1,175

    Export to Excel - Hyperlink columns

    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);
            }
        }

     

    • Post Points: 5
Page 1 of 1 (1 items) | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems