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

Dynmically Created RadGrids, Exporting to Excel

2 Answers 59 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Nick
Top achievements
Rank 1
Nick asked on 02 May 2012, 08:14 PM
This is what I'm trying to achieve:

I have some data that I want to export using a click of a button. When the user clicks the export button, I'm creating mutliple RadGrids dynamically, adding them to the form, and triggering the ExportToExcel() method.

This is part of the code:
For Each Vendor_ID As Integer In hashDataByVendor.Keys
    Dim tempRadGrid As New Telerik.Web.UI.RadGrid
    Dim dtVendor_ID As System.Data.DataTable = hashDataByVendor(Vendor_ID)
    tempRadGrid.DataSource = dtVendor_ID
    Dim vendor As New Vendor(Vendor_ID)
    tempRadGrid.ID = String.Format("{0}-{1}", vendor.Vendor_Name, Date.Now.ToString("MMddyyyy_HHmmssfff"))
    tempRadGrid.MasterTableView.Caption = String.Format("{0} ETA List for {1}", vendor.Vendor_Name, Date.Today.ToString("MM/dd/yyyy"))
 
    Dim tempColumnGrid As New Telerik.Web.UI.GridBoundColumn
    tempRadGrid.MasterTableView.Columns.Add(tempColumnGrid)
    tempColumnGrid.DataField = "Task_ID"
    tempColumnGrid.HeaderText = "Task ID"
    tempColumnGrid.ItemStyle.HorizontalAlign = HorizontalAlign.Left
 
    tempRadGrid.MasterTableView.ShowHeader = True
    tempRadGrid.AutoGenerateColumns = False
    tempRadGrid.ExportSettings.HideStructureColumns = False
    tempRadGrid.ExportSettings.ExportOnlyData = True
    tempRadGrid.ExportSettings.FileName = String.Format("{0} - {1}", vendor.Vendor_Name, Date.Now)
    tempRadGrid.ExportSettings.OpenInNewWindow = True
    Page.Controls.Add(tempRadGrid)
    tempRadGrid.DataBind()
    intTempVendor_ID = vendor.Vendor_ID ' Store the current vendor_id so we can use it in the GridExporting Sub.
    tempRadGrid.MasterTableView.ExportToExcel()
 
    AddHandler tempRadGrid.GridExporting, AddressOf tempRadGrid_GridExporting
    intVendorsCounter += 1
Next

When there is only ONE Grid created through this loop - everything works fine.

When there is more then one Grid created I'm getting an error: 

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

Source Error:

Line 355:                tempRadGrid.ExportSettings.FileName = String.Format("{0} - {1}", vendor.Vendor_Name, Date.Now)
Line 356:                tempRadGrid.ExportSettings.OpenInNewWindow = True
Line 357:                Page.Controls.Add(tempRadGrid)
Line 358:                tempRadGrid.DataBind()
Line 359:                intTempVendor_ID = vendor.Vendor_ID ' Store the current vendor_id so we can use it in the GridExporting Sub.



Any clue on how to solve this matter?

2 Answers, 1 is accepted

Sort by
0
Nick
Top achievements
Rank 1
answered on 04 May 2012, 11:34 AM
Can anybody help? Or maybe suggest a different way of accomplishing this?

I have multiple Data tables created dynamically, I would like to export each data table to Excel.

Nick
0
Daniel
Telerik team
answered on 07 May 2012, 02:14 PM
Hello Nick,

It is not possible to export more than one RadGrid out-of-the-box because the export output is streamed in the response.
Although you could implement this by exporting the control and then refreshing the page (several times in a row), the corresponding code would look a bit "ugly".

Kind regards,
Daniel
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Nick
Top achievements
Rank 1
Answers by
Nick
Top achievements
Rank 1
Daniel
Telerik team
Share this question
or