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