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

Exporting to Excel a RadGrid with some detail tables expanded

2 Answers 752 Views
Grid
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 08 Oct 2014, 06:02 PM
I want to be able to Export to Excel from a RadGrid. The RadGrids on various pages could have 1 detail table or multiple nested detail tables (detail tables inside detail tables.)

I want the user to be able to expand one or more records to view the detail tables and if they export to excel, export it exactly as they see it with paging ignored. There may be multiple expanded detail tables on the same level, or nested tables may be expanded.

Here's the HTML and VB.NET code behind of a fully independent sample page of how our grids are implemented. This has a fake datasource hard coded, we're using LINQ queries to a SQL database in the real website.

This was made in Visual Studio 2012.

Thanks for any help you can offer. We haven't found any help online that has worked.






<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="DetailTableExportSample.WebForm1" %>
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
 
<!DOCTYPE html>
 
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <b>Thanks for helping!</b><br />
        What we want is for one or more of the records in the master (top level) RadGrid to be expanded to display the detail table and then that view, exactly as you see it, to be exportable to Excel.
        <br /><br />
        Below is a simplified example to illustrate our setup. The solution will also need to work if there are multiple detail tables nested (meaning the detail table has it's own detail table).
        <br /><br />
        Please post a response in the forum thread.
 
    </div>
        <br /><br />
        <telerik:RadScriptManager runat="server" ID="RadScriptManager1" />
 
        <telerik:RadGrid AllowSorting="true" ID="rgPeople" runat="server" AutoGenerateColumns="False" Width="500px" CellSpacing="0" GridLines="None">
            <GroupingSettings CaseSensitive="false" />
            <ExportSettings FileName="PM Export" IgnorePaging="true" HideStructureColumns="true" OpenInNewWindow="true" Excel-Format="Html" ExportOnlyData="true" />
            <MasterTableView PageSize="25" DataKeyNames="LName" CommandItemDisplay="Top" AllowPaging="true" AllowSorting="true">
                  <DetailTables>
                       <telerik:GridTableView DataKeyNames="courseID" Name="rgDetail" Width="100%" AllowFilteringByColumn="false">
                           <Columns>
                               <telerik:GridBoundColumn DataField="courseID" HeaderText="Course ID" />
                               <telerik:GridBoundColumn DataField="name" HeaderText="Course" />
                               <telerik:GridBoundColumn DataField="classroom" HeaderText="Room" />
                           </Columns>
                       </telerik:GridTableView>
                  </DetailTables>
                <EditFormSettings EditColumn-InsertText="Add" EditColumn-ButtonType="ImageButton" />
                <CommandItemSettings ShowAddNewRecordButton="false" ShowExportToExcelButton="true" />
                <Columns>
                    <telerik:GridBoundColumn DataField="FName" HeaderText="First" />
                    <telerik:GridBoundColumn DataField="LName" HeaderText="Last" />
                    <telerik:GridBoundColumn DataField="Gender" HeaderText="Gender" />
                </Columns>
                <PagerStyle AlwaysVisible="true" PageSizeControlType="RadComboBox" PageSizes="25,50,100,200,500" />
            </MasterTableView>
        </telerik:RadGrid>
    </form>
</body>
</html>

Imports Telerik.Web.UI
 
Public Class WebForm1
    Inherits System.Web.UI.Page
    Property people As List(Of person)
        Get
            Return ViewState("_people")
        End Get
        Set(value As List(Of person))
            ViewState("_people") = value
        End Set
    End Property
    Property classes As List(Of course)
        Get
            Return ViewState("_classes")
        End Get
        Set(value As List(Of course))
            ViewState("_classes") = value
        End Set
    End Property
 
    Property attendees As List(Of enrollment)
        Get
            Return ViewState("_attendees")
        End Get
        Set(value As List(Of enrollment))
            ViewState("_attendees") = value
        End Set
    End Property
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            MakePeopleList()
            MakeClassesList()
            MakeAttendeesList()
 
            rgPeople.Rebind()
        End If
    End Sub
 
    Private Sub rgPeople_DetailTableDataBind(sender As Object, e As Telerik.Web.UI.GridDetailTableDataBindEventArgs) Handles rgPeople.DetailTableDataBind
        Dim dataItem As GridDataItem = CType(e.DetailTableView.ParentItem, GridDataItem)
        Select Case e.DetailTableView.Name
            Case "rgDetail"
                Dim student As String = dataItem.GetDataKeyValue("LName").ToString
                e.DetailTableView.DataSource = From a In attendees
                                               Join c In classes On c.courseID Equals a.courseID
                                               Where a.student_LName = student
                                               Select c
 
        End Select
    End Sub
 
    Private Sub rgPeople_NeedDataSource(sender As Object, e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles rgPeople.NeedDataSource
        'our actual datasource is a LINQ-SQL query
        If people.Count = 0 Then
            rgPeople.DataSource = Nothing
        Else
            rgPeople.DataSource = From p In people
        End If
    End Sub
 
 
 
#Region "Make_Fake_DataSources"
    Sub MakePeopleList()
        Dim p As New List(Of person)
 
        'people = New List(Of person)
        p.Add(New person("Gary", "Ritter", "Male"))
        p.Add(New person("Bill", "Becker", "Male"))
        p.Add(New person("Michael", "McClary", "Male"))
        p.Add(New person("Angela", "Paulone", "Female"))
        p.Add(New person("Alison", "Weichert", "Female"))
 
        people = p
 
    End Sub
 
    Sub MakeClassesList()
        Dim c As New List(Of course)
        c.Add(New course("HIS101", "American History", "H13"))
        c.Add(New course("ENG101", "English - Freshman", "E10"))
        c.Add(New course("PHY101", "Physical Education", "GYM"))
        c.Add(New course("MTH201", "Algebra 2", "M04"))
        c.Add(New course("SCI106", "Computer Basics", "L03"))
        c.Add(New course("SCI110", "AFJROTC", "S10"))
        classes = c
 
    End Sub
 
    Sub MakeAttendeesList()
        Dim a As New List(Of enrollment)
        a.Add(New enrollment("Ritter", "MTH201"))
        a.Add(New enrollment("Ritter", "SCI110"))
        a.Add(New enrollment("Ritter", "PHY101"))
 
        a.Add(New enrollment("Becker", "SCI106"))
        a.Add(New enrollment("Becker", "HIS101"))
 
        a.Add(New enrollment("McClary", "HIS101"))
        a.Add(New enrollment("McClary", "SCI106"))
        a.Add(New enrollment("McClary", "ENG101"))
 
        a.Add(New enrollment("Paulone", "HIS101"))
        a.Add(New enrollment("Paulone", "PHY101"))
        a.Add(New enrollment("Paulone", "SCI110"))
 
        a.Add(New enrollment("Weichert", "PHY101"))
        a.Add(New enrollment("Weichert", "ENG101"))
        a.Add(New enrollment("Weichert", "MTH201"))
 
        attendees = a
    End Sub
#End Region
End Class
 
 
#Region "Datasource_Classes"
<Serializable>
Public Class person
    Public Property FName As String
    Public Property LName As String
    Public Property Gender As String
 
    Public Sub New(First_Name As String, Last_Name As String, my_Gender As String)
        Me.FName = First_Name
        Me.LName = Last_Name
        Me.Gender = my_Gender
    End Sub
 
    Public Sub New()
    End Sub
End Class
 
<Serializable>
Public Class course
    Public Property courseID As String
    Public Property name As String
    Public Property classroom As String
 
    Public Sub New(id As String, course_name As String, classroom_number As String)
        Me.courseID = id
        Me.name = course_name
        Me.classroom = classroom_number
    End Sub
 
    Public Sub New()
    End Sub
End Class
 
<Serializable>
Public Class enrollment
    Public Property student_LName As String
    Public Property courseID As String
 
    Public Sub New(Last_Name As String, id As String)
        Me.student_LName = Last_Name
        Me.courseID = id
    End Sub
 
    Public Sub New()
    End Sub
End Class
#End Region

2 Answers, 1 is accepted

Sort by
0
Accepted
Eyup
Telerik team
answered on 13 Oct 2014, 12:04 PM
Hello David,

To achieve this requirement, you can implement the approach demonstrated in the following code-library:
http://www.telerik.com/support/code-library/export-hierarchical-grid

Hope this helps.

Regards,
Eyup
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
David
Top achievements
Rank 1
answered on 13 Oct 2014, 08:00 PM
Thanks, that was exactly what I needed!
Tags
Grid
Asked by
David
Top achievements
Rank 1
Answers by
Eyup
Telerik team
David
Top achievements
Rank 1
Share this question
or