Export multiple grids to same Excel file

19 posts, 0 answers
  1. Adam Cole
    Adam Cole avatar
    31 posts
    Member since:
    Nov 2004

    Posted 20 Dec 2008 Link to this post

    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?
  2. Daniel
    Admin
    Daniel avatar
    4943 posts

    Posted 20 Dec 2008 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. LiZ0r
    LiZ0r avatar
    30 posts
    Member since:
    Apr 2009

    Posted 12 May 2009 Link to this post

    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?
  5. Daniel
    Admin
    Daniel avatar
    4943 posts

    Posted 17 May 2009 Link to this post

    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.
  6. LiZ0r
    LiZ0r avatar
    30 posts
    Member since:
    Apr 2009

    Posted 18 May 2009 Link to this post

    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
  7. Daniel
    Admin
    Daniel avatar
    4943 posts

    Posted 21 May 2009 Link to this post

    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.
  8. Mark Galbreath
    Mark Galbreath avatar
    171 posts
    Member since:
    Jul 2009

    Posted 01 Dec 2009 Link to this post

    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
  9. Daniel
    Admin
    Daniel avatar
    4943 posts

    Posted 02 Dec 2009 Link to this post

    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.
  10. Mark Galbreath
    Mark Galbreath avatar
    171 posts
    Member since:
    Jul 2009

    Posted 02 Dec 2009 Link to this post

    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.
  11. Ricardo A. Atencio T.
    Ricardo A. Atencio T. avatar
    2 posts
    Member since:
    Dec 2009

    Posted 11 Dec 2009 Link to this post

    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


     

     

     

     

  12. Mark Galbreath
    Mark Galbreath avatar
    171 posts
    Member since:
    Jul 2009

    Posted 28 Dec 2009 Link to this post

    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

     

     

  13. Kaushik Subramanya
    Kaushik Subramanya avatar
    33 posts
    Member since:
    Aug 2009

    Posted 06 Jan 2010 Link to this post

    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
  14. Mark Galbreath
    Mark Galbreath avatar
    171 posts
    Member since:
    Jul 2009

    Posted 11 Jan 2010 Link to this post

    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
  15. Kaushik Subramanya
    Kaushik Subramanya avatar
    33 posts
    Member since:
    Aug 2009

    Posted 18 Jan 2010 Link to this post

    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

  16. Mark Galbreath
    Mark Galbreath avatar
    171 posts
    Member since:
    Jul 2009

    Posted 19 Jan 2010 Link to this post

    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

  17. Daniel
    Admin
    Daniel avatar
    4943 posts

    Posted 19 Jan 2010 Link to this post

    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.
  18. Kaushik Subramanya
    Kaushik Subramanya avatar
    33 posts
    Member since:
    Aug 2009

    Posted 19 Jan 2010 Link to this post

    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
  19. Mark Galbreath
    Mark Galbreath avatar
    171 posts
    Member since:
    Jul 2009

    Posted 20 Jan 2010 Link to this post

    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

  20. swapnil jagtap
    swapnil jagtap avatar
    4 posts
    Member since:
    May 2010

    Posted 23 Aug 2010 Link to this post

    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
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017