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

Export multiple grids to same Excel file

18 Answers 656 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Adam Cole
Top achievements
Rank 1
Adam Cole asked on 20 Dec 2008, 04:32 PM
Hi,
  I have 3 grids on a page and I want to export all 3 grids to a Excel file.

  Here is the code I used to export the grids to Excel, but it will create 3 Excel files.
            rgReport1.ExportSettings.ExportOnlyData = True
            rgReport1.ExportSettings.OpenInNewWindow = True
            rgReport1.MasterTableView.ExportToExcel()
            rgReport2.ExportSettings.ExportOnlyData = True
            rgReport2.ExportSettings.OpenInNewWindow = True
            rgReport2.MasterTableView.ExportToExcel()
            rgReport3.ExportSettings.ExportOnlyData = True
            rgReport3.ExportSettings.OpenInNewWindow = True
            rgReport3.MasterTableView.ExportToExcel()


            rgReport1.ExportSettings.ExportOnlyData = True
            rgReport1.ExportSettings.OpenInNewWindow = True
            rgReport1.MasterTableView.ExportToExcel()
  Is it possible to export all 3 grids to the same Excel file?

18 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 20 Dec 2008, 06:07 PM
Hello Adam,

The mentioned functionality is not supported out-of-the-box. Actually it's possible to implement it using a simple trick. For your convenience I attached an example to this post.

Hope this helps.

Kind regards,
Daniel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
LiZ0r
Top achievements
Rank 1
answered on 12 May 2009, 11:43 PM
Hi,

I was having the same problem and the example that was provided worked a treat!
Although now I am having problems!
One of the radgrids is being data bound and there are headers and footers and it's causing an error exporting to excel and pdf, would that be the cause of the error? How would I get around that?
0
Daniel
Telerik team
answered on 17 May 2009, 10:57 AM
Hello Lily,

Did you have any tables in your header/footer? I modified the example and included the mentioned additions but I failed to reproduce the problem. My test project is attached to this post.

Best regards,
Daniel
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
LiZ0r
Top achievements
Rank 1
answered on 18 May 2009, 06:38 AM
Hi Daniel,

Each column in my radgrid has a header and a footer.
When I export to Excel, it is fine and I get all my data and headers and footers included.
But when I export to PDF I get an error appearing, which is:
System.InvalidCastException: Unable to cast object of type 'Telerik.Web.Apoc.Layout.BlockArea' to type 'Telerik.Web.Apoc.Layout.AreaContainer'.
  at Telerik.Web.Apoc.Fo.Flow.BlockContainer.Layout(Area area)
  at Telerik.Web.Apoc.Fo.Flow.Block.Layout(Area area)
  at Telerik.Web.Apoc.Fo.Flow.TableCell.Layout(Area area)
  at Telerik.Web.Apoc.Fo.Flow.TableRow.Layout(Area area)
  at Telerik.Web.Apoc.Fo.Flow.AbstractTableBody.Layout(Area area)
  at Telerik.Web.Apoc.Fo.Flow.Table.Layout(Area area)
  at Telerik.Web.Apoc.Fo.Flow.Block.Layout(Area area)
  at Telerik.Web.Apoc.Fo.Flow.Flow.Layout(Area area, Region region)
  at Telerik.Web.Apoc.Fo.Flow.Flow.Layout(Area area)
  at Telerik.Web.Apoc.Fo.Pagination.PageSequence.Format(AreaTree areaTree)
  at Telerik.Web.Apoc.StreamRenderer.Render(PageSequence pageSequence)
  at Telerik.Web.Apoc.Fo.FOTreeBuilder.EndElement()
  at Telerik.Web.Apoc.Fo.FOTreeBuilder.Parse(XmlReader reader)
 

Do you know why I would be able to export to Excel and not PDF?

Regards,
LiZ0r
0
Daniel
Telerik team
answered on 21 May 2009, 12:43 PM
Hello Lily,

As I mentioned previously you probably have a table in your header/footer which causes the PDF export to break. This is the most common cause, but I can't give you to-the-point answer without having the chance to debug your code.

Regards,
Daniel
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Mark Galbreath
Top achievements
Rank 2
answered on 01 Dec 2009, 11:56 AM
Daniel,

I looked at your code and would like a sanity check here.  Your solution to exporting multiple grids on a page into a single Excel file is to wrap all the grids with an additional "master" grid, making the original grids part of the master grid's master table?

Your NeedDataSource() for the master table is:
protected void RadGrid0_NeedDataSource(object source, GridNeedDataSourceEventArgs e){  
    DataTable table = new DataTable();  
    table.Columns.Add("ID");  
    table.Rows.Add("");  
    RadGrid0.DataSource = table;  

I do not understand how this can work with the wrapped grids in your test scenario have 3 columns and 3 rows each, and this is going to return a blank table.

More importantly, however, your code suggests that the various grids must have the same column names and data types?  What about 3 grids, each with different columns and data types?

Cheers!
Mark
0
Daniel
Telerik team
answered on 02 Dec 2009, 05:47 PM
Hello Mark,

Your solution to exporting multiple grids on a page into a single Excel file is to wrap all the grids with an additional "master" grid, making the original grids part of the master grid's master table?
Yes, it is the simplest way to achieve that. It is actually a base from which the users may draw an idea on how to implement this (unsupported) functionality on their own.

I do not understand how this can work with the wrapped grids in your test scenario have 3 columns and 3 rows each, and this is going to return a blank table.
I suppose you already know that RadGrid doesn't support unbound mode. Therefore I created a single empty item that contains an empty string. This represents the wrapper - it doesn't need any data.
If you run the example locally you will notice that it will works flawlessly. For the sake of simplicity, the GetData method populates both RadGrids in this project. In a real scenario you will have a separate NeedDataSource events/DataSource controls.

More importantly, however, your code suggests that the various grids must have the same column names and data types?  What about 3 grids, each with different columns and data types?
I believe I already answered this question.

Let me know if I'm missing something.

Best regards,
Daniel
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Mark Galbreath
Top achievements
Rank 2
answered on 02 Dec 2009, 05:57 PM
Okay, Daniel's example inspired me.  Here's how to aggregate any data that is dynamically delivered to the view and export it to an Excel worksheet.  My example aggregates 4 HTML tables populated with data delivered in ASP labels and 4 RadGrids each displaying data from a different table with different schemas.

First, stick an empty RadGrid tag at the bottom of the webform displaying the data, along with a button (ASP is simplest):

<asp:Button ID="btn_Export" runat="server" /> 
<telerik:RadGrid ID=RadGrid0 runat=server></telerik:RadGrid> 

Next, in your code-behind (I am using VB, but porting to C# is trivial), build a virtual datatable based on all the data items displayed in the view:

Protected Function GetData() As DataTable  
    Dim colLIN As New DataColumn( "LIN", GetType( String ))  
    Dim colLIN_nomen As DataColumn = New DataColumn( "LIN_nomen", GetType( String ))  
    colLIN_nomen.AllowDBNull = True 
 
    Dim colLIN_commodity As DataColumn = New DataColumn( "LIN_commodity", GetType( String ))  
    colLIN_commodity.AllowDBNull = True 
 
    Dim colLIN_capability As DataColumn = New DataColumn( "LIN_capability", GetType( String ))  
    colLIN_capability.AllowDBNull = True 
 
    Dim colLIN_family As DataColumn = New DataColumn( "LIN_family", GetType( String ))  
    colLIN_family.AllowDBNull = True 
 
    Dim colLIN_sub_family As DataColumn = New DataColumn( "LIN_sub_family", GetType( String ))  
    colLIN_sub_family.AllowDBNull = True 
 
    Dim colLIN_sup_class As DataColumn = New DataColumn( "LIN_sup_class", GetType( Int32 ))  
    colLIN_sup_class.AllowDBNull = True 
 
    Dim colLIN_price As DataColumn = New DataColumn( "LIN_price", GetType( String ))  
    colLIN_price.AllowDBNull = True 
 
    Dim colMTOE_IBCT As DataColumn = New DataColumn( "MTOE_IBCT", GetType( Int32 ))  
    colMTOE_IBCT.AllowDBNull = True   
 
'**** you get the idea...now instantiate a DataTable ****  
 
    Dim table As DataTable = new DataTable()  
    table.Columns.AddRange( New DataColumn() { colLIN, colLIN_nomen, colLIN_commodity, colLIN_capability, colLIN_family, _  
    colLIN_sub_family, colLIN_sup_class, colLIN_price, colARI_transaction, _  
 
'**** etc. for the rest of the fields ****  
'**** now instantiate the table rows using the table object ****  
'**** first are the data from the ASP labels, then loop through the ****  
'**** radGrids ****  
    })  
 
    Dim row As DataRow = table.NewRow()  
    row( 0 ) = lbl_lin_data.Text  
    row( 1 ) = lbl_lin_nomen_data.Text  
    row( 2 ) = lbl_commodity_data.Text  
    row( 3 ) = lbl_capability_data.Text  
    row( 4 ) = lbl_family_data.Text  
    row( 5 ) = lbl_sub_family_data.Text  
 
'**** This needs to be tested for nulls ****  
    row( 6 ) = CType( lbl_sup_class_data.Text, Int32 )  
 
'**** figure out how many interations to get multiple records from each field ****  
    Dim temp1 As Int32 = Math.Max( radGrid1.Items.Count, radGrid2.Items.Count )  
    Dim temp2 As Int32 = Math.Max( radGrid3.Items.Count, radGrid4.Items.Count )  
    Dim cnt As Int32 = Math.Max( temp1, temp2 )  
    Dim i As Int32 = 0 
 
'**** the first 2 cells (cell( 0 ) and cell( 1 ) in a radGrid are occupied by &nbsp; ****  
 
    For i = 1 to cnt  
        If radGrid1.Items.Count >= i Then   
            row( 18 ) = radGrid1.Items.Item( i - 1 ).Cells( 2 ).Text   
            row( 19 ) = radGrid1.Items( i - 1 ).Cells( 3 ).Text  
            row( 20 ) = radGrid1.Items.Item( i - 1 ).Cells( 4 ).Text  
        End If  
 
        If radGrid2.Items.Count >= i Then  
            row( 21 ) = radGrid2.Items.Item( i - 1 ).Cells( 2 ).Text   
            row( 22 ) = radGrid2.Items( i - 1 ).Cells( 3 ).Text   
            row( 23 ) = Ctype( radGrid2.Items.Item( i - 1 ).Cells( 4 ).Text, Int32 )  
            row( 24 ) = radGrid2.Items.Item( i - 1 ).Cells( 5 ).Text   
        End If  
 
' **** etc. for the rest of the grids ****  
 
        table.Rows.Add( row )  
        row = table.NewRow()  
    Next i  
 
    Return table  
End Function  

And finally, dynamically build a RadGrid to bind to the virtual table you just built, and export:

Protected Sub btn_export_Click( sender As Object, e As EventArgs ) Handles btn_export.Click  
    Dim colLIN As GridBoundColumn = New GridBoundColumn()  
    radGrid0.MasterTableView.Columns.Add( colLIN )  
    colLIN.UniqueName = "LIN" 
    colLIN.DataField = "colLIN" 
 
    Dim colLIN_nomen = New GridBoundColumn()  
    radGrid0.MasterTableView.Columns.Add( colLIN_nomen )  
    colLIN_nomen.UniqueName = "LIN_nomen" 
    colLIN_nomen.DataField = "colLIN_nomen" 
 
    Dim colLIN_commodity As GridBoundColumn = New GridBoundColumn()  
    radGrid0.MasterTableView.Columns.Add( colLIN_commodity )  
    colLIN_commodity.UniqueName = "commodity" 
    colLIN_commodity.DataField = "colLIN_commodity" 
 
'**** etc. for the rest of the data fields ****  
'**** now the usual export enumerations ****  
 
    radGrid0.ExportSettings.IgnorePaging = True   
    radGrid0.ExportSettings.OpenInNewWindow = True 
    radGrid0.ExportSettings.ExportOnlyData = True 
    radGrid0.ExportSettings.Excel.Format = GridExcelExportFormat.ExcelML   
 
'**** and bind the grid and export ****  
 
    RadGrid0.DataSource = GetData()  
    RadGrid0.DataBind()  
 
    radGrid0.MasterTableView.ExportToExcel   
End Sub  
 

And that's it.
0
Ricardo A. Atencio T.
Top achievements
Rank 1
answered on 11 Dec 2009, 04:08 PM
Hello, i have a problem...

I need this code in VB.Net

 

protected void RadGrid2_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)

 

{

(source

as RadGrid).DataSource = GetData1(2);

 

 

 

}

I try this line:

Protected Sub rgServicios_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs)

 (source as RadGrid).DataSource = GetData1(2)

 

End Sub

But, say : "Syntax error"...

Please somebody to say code in vb.net

Thank you


 

 

 

 

0
Mark Galbreath
Top achievements
Rank 2
answered on 28 Dec 2009, 08:15 PM

Your C# method:

protected void RadGrid2_NeedDataSource( object source, GridNeedDataSourceEventArgs e ) {  
    ( source as RadGrid ).DataSource = GetData1( 2 );   

does not translate to your VB sub:

Protected Sub rgServicios_NeedDataSource( source As Object, e As GridNeedDataSourceEventArgs )

    ( source as RadGrid ).DataSource = GetData1( 2 )
End Sub

First, your method referes to one object, RadGrid2, and your sub referes to another, rgServicios.  Second, the body of your sub should be refactored to

rgServicios.DataSource = GetData1( 2 ) 

assuming your VB sub or function, GetData1( 2 ), declares a datasource and binds rgServicios to it.

Cheers!
Mark

 

 

0
Kaushik Subramanya
Top achievements
Rank 1
answered on 06 Jan 2010, 06:26 PM
Mark,

I am not clear about your last post.
I have 3 Radgrid rg1, rg2 and rg3.

Following your steps I created RadGrid0

(source as RadGrid).DataSource = GetData(2);


What should be the value for (source as RadGrid) ?

Would you happen to have a sample code ?  I am looking to export 3 radgrids along with 3 html tables

Your help is much appreciated

Regards,
Kaushik
0
Mark Galbreath
Top achievements
Rank 2
answered on 11 Jan 2010, 12:49 PM
Kaushik wrote:
(source as RadGrid).DataSource = GetData(2);  
What should be the value for (source as RadGrid) ?  
 

rg1.DataSource = GetData( 1 )
rg1.DataBind()
rg2.DataSource = GetData( 2 )
rg2.DataBind()
rg3.DataSource = GetData( 3 )
rg3.DataBind()

I always encapsulate my databinding method calls inside a VB Load method for data grids, like:
'''  
''' <summary>Initialize the radgrid</summary> 
'''  
Protected Sub rg1_Load( source As Object, e As EventArgs) Handles rg1_Load  
    rg1.DataSource = getData( 1 )  
    rg1.DataBind()  
End Sub  
 
'''  
''' Method automatically rebinds grid to datasource as needed  
'''  
Protect Sub rg1_NeedDataSource( source As Object, e As GridNeedDataSourceEventArgs ) Handles rg1_NeedDataSource  
    rg1.DataSource = getData( 1 )  
End Sub 

Do this for each of your radgrids.  I am assuming your getData() method calls an appropriate DAO (data access object, or "object data source" in VB parlence) based on the passed method argument.

Cheers!
Mark

Cheers!
Mark
0
Kaushik Subramanya
Top achievements
Rank 1
answered on 18 Jan 2010, 03:58 PM

Mark/Daniel,
I tried to implement the solution suggested but I am getting compile time error.
The name 'rg1' does not exist in the current context 
The name 'Label1' does not exist in the current context 
The name 'rg2' does not exist in the current context 
The name ''Label2' does not exist in the current context 
The name 'rg3' does not exist in the current context 
The name ''Label3' does not exist in the current context 

Can you please advise on where I am doing wrong

ASPX Code

<telerik:RadGrid ID="rgWrapper" runat="server" ShowHeader="false" BorderStyle="None" OnNeedDataSource="rgWrapper_NeedDataSource">     
                <ExportSettings OpenInNewWindow="true" />    
                    <MasterTableView AutoGenerateColumns="true">     
                        <ItemTemplate>    
                            <asp:Label ID="Label1" runat="server" Text=""></asp:Label>    
                            <telerik:RadGrid ID="rg1" runat="server"  AutoGenerateColumns="False"    
                                    GridLines="None" OnNeedDataSource="rg1_NeedDataSource" OnItemDataBound="rg1_ItemDataBound">     
                                    <MasterTableView>    
                                        <Columns>    
                                            <telerik:GridTemplateColumn HeaderText="Name" UniqueName="Name" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" HeaderStyle-Font-Bold="True">     
                                                <ItemTemplate>    
                                                    <%#Eval("FirstName")%>    
                                                    <%#Eval("LastName")%>    
                                                </ItemTemplate>    
                                            </telerik:GridTemplateColumn>    
                                            <telerik:GridBoundColumn HeaderText="Date " UniqueName="Date" DataField="DateEntered"    
                                                HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" HeaderStyle-Font-Bold="True">     
                                            </telerik:GridBoundColumn>    
                                        </Columns>    
                                    </MasterTableView>    
                            </telerik:RadGrid>    
                            <br />    
                            <asp:Label ID="Label2" runat="server"></asp:Label>    
                            <telerik:RadGrid ID="rg2" runat="server"  AutoGenerateColumns="False"    
                                    GridLines="None" OnNeedDataSource="rg2_NeedDataSource" OnItemDataBound="rg2_ItemDataBound">     
                                    <MasterTableView>    
                                        <Columns>    
                                            <telerik:GridTemplateColumn HeaderText="Name" UniqueName="Name" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" HeaderStyle-Font-Bold="True">     
                                                <ItemTemplate>    
                                                    <%#Eval("FirstName")%>    
                                                    <%#Eval("LastName")%>    
                                                </ItemTemplate>    
                                            </telerik:GridTemplateColumn>    
                                            <telerik:GridBoundColumn DataField="DateEntered" HeaderText="Date" UniqueName="Date"    
                                                DataFormatString="{0:dd/MM/yyyy}" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" HeaderStyle-Font-Bold="True">     
                                            </telerik:GridBoundColumn>    
                                        </Columns>    
                                    </MasterTableView>    
                            </telerik:RadGrid>    
                            <br />    
                            <asp:Label ID="Label3" runat="server" ></asp:Label>    
                            <telerik:RadGrid ID="rg3" runat="server"      
                                    AutoGenerateColumns="False" GridLines="None"      
                                    OnNeedDataSource="rg3_NeedDataSource"      
                                    onitemdatabound="rg3_ItemDataBound">     
                                    <MasterTableView>    
                                        <Columns>    
                                            <telerik:GridTemplateColumn HeaderText="Name" UniqueName="Name" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" HeaderStyle-Font-Bold="True">     
                                            <ItemTemplate>    
                                                    <%#Eval("FirstName")%>    
                                                    <%#Eval("LastName")%>    
                                                </ItemTemplate>    
                                            </telerik:GridTemplateColumn>    
                                            <telerik:GridBoundColumn HeaderText="Date" UniqueName="Date"      
                                                DataField="LastModifiedDate" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" HeaderStyle-Font-Bold="True">     
                                            </telerik:GridBoundColumn>    
                                        </Columns>    
                                    </MasterTableView>    
                            </telerik:RadGrid>    
                    </ItemTemplate>    
                </MasterTableView>    
            </telerik:RadGrid>    
 

,cs Code

protected void rgWrapper_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)     
{     
     rgWrapper.DataSource = new string[] { " " };     
}   
 
 
protected void rg1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)  
{  
     DataTable dt = GetData(1);  
     rgPlanVersionHistory.DataSource = dt;  
     Label1.Text = (dt.Rows.Count > 0)  
                 ? "DataFound"  
                 : "ALERT: No records found!";  

Regards,
Kaushik

0
Mark Galbreath
Top achievements
Rank 2
answered on 19 Jan 2010, 02:28 PM

Kaushik,

Daniel can probably answer this better than I, but the reason you are getting those compile time errors is because you have those controls wrapped inside your master grid (rgWrapper).  For reasons unknown to me, Telerik (or Microsoft) does not allow nested controls to be seen in the code-behind.  The solution is to create all your controls dynamically in the code-behind, a royal PITA.

 

Cheers!

Mark

0
Daniel
Telerik team
answered on 19 Jan 2010, 04:05 PM
Hello Kaushik,

Indeed, Mark is right - you can't access a control nested in a naming container directly.

Please examine the following links:
MSDN: How to: Access Members of a Control's Naming Container
Joteke's Blog: Understanding the naming container hierarchy of ASP.NET databound controls

Dummy code:
Label myLabel = rgWrapper.Items[0].FindControl("Label1") as Label;

Best regards,
Daniel
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Kaushik Subramanya
Top achievements
Rank 1
answered on 20 Jan 2010, 03:19 AM
Daniel/Mark,
Thank you for your tip. It was very helpful.

However I am facing an interesting issue.
Based on your recommendation I modified the code as below.

ASPX page
<telerik:RadGrid ID="rgWrapper" runat="server" ShowHeader="false" BorderStyle="None" OnNeedDataSource="rgWrapper_NeedDataSource">        
                <ExportSettings OpenInNewWindow="true" />       
                    <MasterTableView AutoGenerateColumns="true">        
                        <ItemTemplate>       
                            <asp:Label ID="Label1" runat="server" Text=""></asp:Label>       
                            <telerik:RadGrid ID="rg1" runat="server"  AutoGenerateColumns="False"       
                                    GridLines="None" OnNeedDataSource="rg1_NeedDataSource" OnItemDataBound="rg1_ItemDataBound">        
                                    <MasterTableView>       
                                        <Columns>       
                                            <telerik:GridTemplateColumn HeaderText="Name" UniqueName="Name" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" HeaderStyle-Font-Bold="True">        
                                                <ItemTemplate>       
                                                    <%#Eval("FirstName")%>       
                                                    <%#Eval("LastName")%>       
                                                </ItemTemplate>       
                                            </telerik:GridTemplateColumn>       
                                            <telerik:GridBoundColumn HeaderText="Date " UniqueName="Date" DataField="DateEntered"       
                                                HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" HeaderStyle-Font-Bold="True">        
                                            </telerik:GridBoundColumn>       
                                        </Columns>       
                                    </MasterTableView>       
                            </telerik:RadGrid>       
                            <br />       
                            <asp:Label ID="Label2" runat="server"></asp:Label>       
                            <telerik:RadGrid ID="rg2" runat="server"  AutoGenerateColumns="False"       
                                    GridLines="None" OnNeedDataSource="rg2_NeedDataSource" OnItemDataBound="rg2_ItemDataBound">        
                                    <MasterTableView>       
                                        <Columns>       
                                            <telerik:GridTemplateColumn HeaderText="Name" UniqueName="Name" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" HeaderStyle-Font-Bold="True">        
                                                <ItemTemplate>       
                                                    <%#Eval("FirstName")%>       
                                                    <%#Eval("LastName")%>       
                                                </ItemTemplate>       
                                            </telerik:GridTemplateColumn>       
                                            <telerik:GridBoundColumn DataField="DateEntered" HeaderText="Date" UniqueName="Date"       
                                                DataFormatString="{0:dd/MM/yyyy}" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" HeaderStyle-Font-Bold="True">        
                                            </telerik:GridBoundColumn>       
                                        </Columns>       
                                    </MasterTableView>       
                            </telerik:RadGrid>       
                            <br />       
                            <asp:Label ID="Label3" runat="server" ></asp:Label>       
                            <telerik:RadGrid ID="rg3" runat="server"         
                                    AutoGenerateColumns="False" GridLines="None"         
                                    OnNeedDataSource="rg3_NeedDataSource"         
                                    onitemdatabound="rg3_ItemDataBound">        
                                    <MasterTableView>       
                                        <Columns>       
                                            <telerik:GridTemplateColumn HeaderText="Name" UniqueName="Name" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" HeaderStyle-Font-Bold="True">        
                                            <ItemTemplate>       
                                                    <%#Eval("FirstName")%>       
                                                    <%#Eval("LastName")%>       
                                                </ItemTemplate>       
                                            </telerik:GridTemplateColumn>       
                                            <telerik:GridBoundColumn HeaderText="Date" UniqueName="Date"         
                                                DataField="LastModifiedDate" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" HeaderStyle-Font-Bold="True">        
                                            </telerik:GridBoundColumn>       
                                        </Columns>       
                                    </MasterTableView>       
                            </telerik:RadGrid>       
                    </ItemTemplate>       
                </MasterTableView>       
            </telerik:RadGrid>       
    
 

.CS Page
protected void btnExportToExcel_Click(object sender, EventArgs e)  
{  
   rgWrapper.ExportSettings.FileName = "CombinedExcelReport";  
   rgWrapper.MasterTableView.ExportToExcel();  
}  
 
protected void rgWrapper_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)  
{  
   rgWrapper.DataSource = new string[] { " " };  
}  
 
 
protected void rg1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)  
{  
     Label lbl1 = rgWrapper.Items[0].FindControl("Label1") as Label;  
     RadGrid rg_1 = rgWrapper.Items[0].FindControl("rg1") as RadGrid;  
 
     DataTable dt = GetData(1);  
     if (dt.Columns[0].Rows.Count > 0)  
     {  
         lbl1.Text = "Success";  
         rg_1.DataSource = GetData(1);  
     }  
     else  
     {  
         lbl1.Text = "No rows found";  
     }  
}  
 
protected void rg2_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)  
{  
     Label lbl2 = rgWrapper.Items[0].FindControl("Label2") as Label;  
     RadGrid rg_2 = rgWrapper.Items[0].FindControl("rg2") as RadGrid;  
 
     DataTable dt = GetData(2);  
     if (dt.Columns[0].Rows.Count > 0)  
     {  
         lbl2.Text = "Success";  
         rg_2.DataSource = GetData(2);  
     }  
     else  
     {  
         lbl2.Text = "No rows found";  
     }  
}  
 
protected void rg3_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)  
{  
     Label lbl3 = rgWrapper.Items[0].FindControl("Label3") as Label;  
     RadGrid rg_3 = rgWrapper.Items[0].FindControl("rg3") as RadGrid;  
 
     DataTable dt = GetData(3);  
     if (dt.Columns[0].Rows.Count > 0)  
     {  
         lbl3.Text = "Success";  
         rg_3.DataSource = GetData(3);  
     }  
     else  
     {  
         lbl3.Text = "No rows found";  
     }  
}  
 

I am able to see the values for lbl1, lbl2 lbl3, rg_1 etc while debugging but when the page shows up I do not see anything. Please advise.

Regards,
Kaushik
0
Mark Galbreath
Top achievements
Rank 2
answered on 20 Jan 2010, 12:15 PM
Kaushik,

When you use 'As' to cast an object type in C#, if the cast is illegal, 'As' simply returns null and does not throw an exception.  The syntax you have for getting references to your wrapped objects:

Label lbl1 = rgWrapper.Items[0].FindControl("Label1"as Label;  
RadGrid rg_1 = rgWrapper.Items[0].FindControl("rg1"as RadGrid;   
 

does not need a passive cast.  Either do away with the cast altogether or use an explicit cast:

Label lbl1 = ( Label ) rgWrapper.Items[0].FindControl("Label1");  
RadGrid rg_1 = ( RadGrid ) rgWrapper.Items[0].FindControl("rg1");    
 

An explicit cast will raise an exception you can catch and examine the stack trace.

Further, you are not using NeedDataSource() correctly.  The only thing that this method should do is reassign the datasource (radGrid1.DataSource = myDataSource).  You are not assigning the datasource in any of your 3 methods, so NeedDataSource() is not going to Rebind() your controls.

 

Cheers!

Mark

0
swapnil jagtap
Top achievements
Rank 1
answered on 23 Aug 2010, 08:07 AM
Hi,

I have done successful multiple Radgrid export to excel with this example, but only issue is that two row appending after each grid at footer. Actually  I dont want any row at footer.
Please reply me if you have solution on this.

Reagards,
Swapnil P. Jagtap
Tags
Grid
Asked by
Adam Cole
Top achievements
Rank 1
Answers by
Daniel
Telerik team
LiZ0r
Top achievements
Rank 1
Mark Galbreath
Top achievements
Rank 2
Ricardo A. Atencio T.
Top achievements
Rank 1
Kaushik Subramanya
Top achievements
Rank 1
swapnil jagtap
Top achievements
Rank 1
Share this question
or