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

Export Single Detail Grid table in RadGrid

1 Answer 224 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Vinod
Top achievements
Rank 1
Vinod asked on 16 Jul 2015, 02:37 PM

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

1 Answer, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 21 Jul 2015, 07:10 AM
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
Tags
Grid
Asked by
Vinod
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Share this question
or