This is a migrated thread and some comments may be shown as answers.

Having trouble with exporting to excel (blank column)

2 Answers 437 Views
Grid
This is a migrated thread and some comments may be shown as answers.
JenMaryland
Top achievements
Rank 1
JenMaryland asked on 04 Apr 2018, 07:57 PM

I'm testing out the radgrid functionality and I'm trying to export data to excel. This works fine in having it export to an actual Excel spreadsheet instead of as an HTML file. The problem I'm running into is that I want one column to be a LinkButton. In testing, this would not show up when I exported to excel. I then read about calling "NeedsDataSource", which does show the column that has the linkbutton (shows it as text in the excel spreadsheet). The problem is, it only shows the current page (paging is on). But, if I remove the function, then I will get all pages, but no text in the buttonlink column. What I want it to do is:

1 - For the ID column, display a link to a separate page. This works ok, although I would rather have it open to a new window as opposed to a new tab.
2 - When exporting, display the text in for the column with the linkbutton, and display all pages.

I'm not sure how to proceed.

 

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="RadGridTest.aspx.vb" Inherits="Test.RadGridTest" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" AllowSorting="True" PageSize="25" ShowFooter="True" ShowStatusBar="True">
<GroupingSettings CollapseAllTooltip="Collapse all groups"></GroupingSettings>
<ExportSettings IgnorePaging="True">
<Excel FileExtension="xlsx" Format="Xlsx" />
</ExportSettings>
</telerik:RadGrid>
</div>
<telerik:RadScriptManager ID="RadScriptManager1" runat="server">
</telerik:RadScriptManager>
<telerik:RadButton ID="RadButton1" runat="server" Skin="Default" Text="RadButton">
</telerik:RadButton>
</form>
</body>
</html>

 

Imports Telerik.Web.UI
Public Class RadGridTest
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Bind()
End If
End Sub
Protected Sub RadButton1_Click(sender As Object, e As EventArgs) Handles RadButton1.Click
Dim alternateText As String = "Xlsx"
RadGrid1.ExportSettings.Excel.Format = DirectCast([Enum].Parse(GetType(GridExcelExportFormat), alternateText), GridExcelExportFormat)
RadGrid1.ExportSettings.IgnorePaging = True
RadGrid1.ExportSettings.ExportOnlyData = False
RadGrid1.ExportSettings.OpenInNewWindow = True
RadGrid1.MasterTableView.ExportToExcel()
End Sub
Private Sub RadGrid1_PreRender(sender As Object, e As EventArgs) Handles RadGrid1.PreRender
For Each item As GridDataItem In RadGrid1.MasterTableView.Items
Dim link As New HyperLink
link.Text = item("PrimaryKeyValue").Text
link.NavigateUrl = "http://www.google.com/" & item("PrimaryKeyValue").Text
link.Target = "_blank"
If True Then
item("PrimaryKeyValue").Controls.Add(link)
End If
Next
End Sub
Private Sub RadGrid1_SortCommand(sender As Object, e As GridSortCommandEventArgs) Handles RadGrid1.SortCommand
Bind()
End Sub
Private Sub Bind()
RadGrid1.DataSource = dbData.getDataTable("SELECT PrimaryKeyValue FROM _GridTest", SQLDataClass.GetConnection())
End Sub
Private Sub RadGrid1_PageIndexChanged(sender As Object, e As GridPageChangedEventArgs) Handles RadGrid1.PageIndexChanged
Bind()
End Sub
Private Sub RadGrid1_PageSizeChanged(sender As Object, e As GridPageSizeChangedEventArgs) Handles RadGrid1.PageSizeChanged
Bind()
End Sub
Private Sub RadGrid1_NeedDataSource(sender As Object, e As GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource
Bind()
' When Bind() is called in this event, then the export will not show the link button field, but will show all pages.
' When Bind() is not called, the export will show the link button field, but only for the current page.
End Sub
End Class

2 Answers, 1 is accepted

Sort by
0
JenMaryland
Top achievements
Rank 1
answered on 06 Apr 2018, 06:49 PM
BTW, the version I'm using is 2016.1.225.45. For now, I've found a work-around by simply making a "ReadyForPrint" variable which is set to true when the export button is clicked, then gets set to false when the page loads. The linkbutton rendering only happens when ReadyForPrint is false.
0
Eyup
Telerik team
answered on 09 Apr 2018, 06:18 AM
Hello Jennifer,

Please note that there is no need to bind the grid on built-in actions. The grid handles them automatically:
https://www.telerik.com/support/kb/aspnet-ajax/grid/details/how-to-bind-radgrid-properly-on-server-side

As for the exporting requirement, I am sending a sample RadGrid web site to demonstrate various ways of declaring link columns which can be exported to excel. Please run the attached application and let me know if it helps you.

Regards,
Eyup
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Grid
Asked by
JenMaryland
Top achievements
Rank 1
Answers by
JenMaryland
Top achievements
Rank 1
Eyup
Telerik team
Share this question
or