RadGrid Export to XLSX with GridHyperlinkColumn

4 posts, 1 answers
  1. Beth
    Beth avatar
    5 posts
    Member since:
    Jan 2014

    Posted 03 Feb Link to this post

    Hello,

    I have a RadGrid for which I have enabled exporting to Excel using the XSLX format. The export works very well except the export removes hyperlinks from the grid and replaces them with plain text. I have a requirement to include in the resulting XSLX file the hyperlink that appears in the grid in a GridHyperlinkColumn. I have tried altering the ExportOnlyData value in the grid's export settings, but that causes the data in the GridHyperlinkColumn to not appear at all. Here is a simplified grid. Thank you for your advice on whether XSLX exports can include hyperlinks.

      <telerik:RadGrid ID="rgInstSearchResults" runat="server" RenderMode="Lightweight"
            DataSourceID="odsInstSearch" AutoGenerateColumns="false" Skin="Bootstrap"
            ShowStatusBar="true" AllowMultiRowSelection="true" Width="95%" GridLines="Both"
            AllowFilteringByColumn="false">
            <ClientSettings>
                <Selecting AllowRowSelect="true" UseClientSelectColumnOnly="true" />
            </ClientSettings>
            <ExportSettings ExportOnlyData="true" OpenInNewWindow="true" FileName="InstitutionSearchResults" IgnorePaging="true">
                <Excel DefaultCellAlignment="Left" Format="Xlsx" />
                <Word Format="Html" />
            </ExportSettings>
            <MasterTableView Name="mtvInstSearchResults" DataSourceID="odsInstSearch"
                DataKeyNames="InstitutionID" CommandItemDisplay="Top" AllowPaging="true"
                AllowSorting="true" PageSize="50" AllowMultiColumnSorting="true" AllowAutomaticDeletes="true"
                ShowFooter="True" AllowAutomaticInserts="False" AllowAutomaticUpdates="False">
                <CommandItemStyle BackColor="#dae0ed" Font-Bold="true" HorizontalAlign="Right" />
                <CommandItemSettings ShowExportToExcelButton="true" ShowAddNewRecordButton="false" ShowRefreshButton="true"
                    ShowExportToWordButton="true" />
                <PagerStyle AlwaysVisible="true" PageSizes="25,50,100,500" />
                <Columns>
                    <telerik:GridClientSelectColumn UniqueName="ClientSelectColumn" Reorderable="False" Exportable="false" />
                    <telerik:GridHyperLinkColumn UniqueName="InstitutionID" HeaderText="ID"
                        DataNavigateUrlFields="InstitutionID" Reorderable="False"
                        DataNavigateUrlFormatString="ViewInstitution.aspx?InstitutionID={0}"
                        DataTextField="InstitutionID" Target="_blank" />
    </Columns>
      </MasterTableView>
        </telerik:RadGrid>

     

  2. Answer
    Eyup
    Admin
    Eyup avatar
    4027 posts

    Posted 06 Feb Link to this post

    Hi Beth,

     

    You can achieve this requirement using the following approach:

    <ExportSettings ExportOnlyData="true">
    C#:

        protected void RadGrid1_InfrastructureExporting(object sender, GridInfrastructureExportingEventArgs e)
        {
            Telerik.Web.UI.ExportInfrastructure.Table table = e.ExportStructure.Tables[0];
            for (int col = 0; col < RadGrid1.Columns.Count; col++)
            {
                if (RadGrid1.Columns[col].UniqueName == "InstitutionID")
                {
                    for (int row = 1; row <= table.Rows.Count; row++)
                    {
                        table.Cells[col + 1, row].Hyperlink = "https://en.wikipedia.org";
                    }
                    break;
                }
            }
        }

    I hope this will prove helpful.

     

    Regards,
    Eyup
    Progress Telerik

    Get quickly onboarded and successful with UI for ASP.NET AJAX with the Virtual Classroom technical trainings, available to all active customers. Learn More.
  3. Beth
    Beth avatar
    5 posts
    Member since:
    Jan 2014

    Posted 14 Feb in reply to Eyup Link to this post

    This worked perfectly! Thank you very much. 

    How would this same task work when exporting to Word (HTML)? 

  4. Beth
    Beth avatar
    5 posts
    Member since:
    Jan 2014

    Posted 14 Feb in reply to Beth Link to this post

    Please disregard the question, I figured out how to do it. For anyone looking at this question, I used the radgrid's ExportCellFormatting event:

        Private Sub myGrid_ExportCellFormatting(sender As Object, e As ExportCellFormattingEventArgs) Handles myGrid.ExportCellFormatting
            Dim myItem As GridDataItem = e.Cell.Parent
            Dim currInst As Institution = myItem.DataItem
            Dim myColumn As GridColumn = e.FormattedColumn
            If myColumn.UniqueName = "InstitutionID" Then
                Dim url As String = SystemConfig.baseURL + "Institutions/ViewInstitution.aspx?InstitutionID=" + currInst.InstitutionID.ToString()
                e.Cell.Text = "<a href='" + url + "'>" + currInst.InstitutionID.ToString() + "</a>"
            End If
        End Sub

     

     

Back to Top