Export Single Detail Grid table in RadGrid

2 posts, 0 answers
  1. Vinod
    Vinod avatar
    1 posts
    Member since:
    Dec 2012

    Posted 16 Jul 2015 Link to this post

    I am using RadGrid and showing 2 level hierarchial data master and detail.

    I want to export just the detail table of a single master row to Excel

    Is it possible to do it.

    Note: I can export the complete Grid to Excel and it works.

    Thanks,

    Vinod

  2. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    2447 posts

    Posted 21 Jul 2015 Link to this post

    Hi Vinod,

    For exporting only a particular Detail Table (GridTableView) you could get reference to that object and call its ExportToExcel method. 

    For your convenience, following is an example with such implementation:
    <telerik:RadGrid runat="server" ID="RadGrid1" OnNeedDataSource="RadGrid1_NeedDataSource" OnDetailTableDataBind="RadGrid1_DetailTableDataBind"
         OnItemCommand="RadGrid1_ItemCommand">
        <MasterTableView HierarchyDefaultExpanded="true">
            <Columns>
                <telerik:GridTemplateColumn>
                    <ItemTemplate>
                        <telerik:RadButton runat="server" ID="RadButton1" CommandName="ExportDetailTable" Text="Export first detail table"></telerik:RadButton>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
            </Columns>
            <DetailTables>
                <telerik:GridTableView Name="DTable1"></telerik:GridTableView>
                <telerik:GridTableView Name="DTable2"></telerik:GridTableView>
            </DetailTables>
        </MasterTableView>
    </telerik:RadGrid>

    And the code-behind:
    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        DataTable table = new DataTable();
        table.Columns.Add("ID", typeof(int));
        table.Columns.Add("MasterColumn", typeof(string));
        for (int i = 0; i < 5; i++)
        {
            table.Rows.Add(i, "MasterColumnValue");
        }
     
        (sender as RadGrid).DataSource = table;
    }
     
    protected void RadGrid1_DetailTableDataBind(object sender, GridDetailTableDataBindEventArgs e)
    {
        DataTable table = new DataTable();
     
        if (e.DetailTableView.Name == "DTable1")
        {
            table.Columns.Add("ID", typeof(int));
            table.Columns.Add("DTable1Column", typeof(string));
            for (int i = 0; i < 5; i++)
            {
                table.Rows.Add(i, "dtable1Value");
            }
        }
        else
        {
            table.Columns.Add("ID", typeof(int));
            table.Columns.Add("DTable2Column", typeof(string));
            for (int i = 0; i < 5; i++)
            {
                table.Rows.Add(i, "dtable2Value");
            }
        }
     
        e.DetailTableView.DataSource = table;
     
    }
     
    protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == "ExportDetailTable" && e.Item is GridDataItem && e.Item.HasChildItems)
        {
            (e.Item as GridDataItem).ChildItem.NestedTableViews[0].ExportToExcel();
        }
    }

    Hope this helps.


    Regards,
    Konstantin Dikov
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top