ExportSettings in DetailTables

13 posts, 0 answers
  1. Danny
    Danny avatar
    29 posts
    Member since:
    Feb 2012

    Posted 03 Feb 2012 Link to this post

    Hi,

    I'm having troubles using the export on my detail tables. In my page I've placed one RagGrid with a MasterTableView, in it is a detailTable and in there is another detailTable. I've set some export settings in the grid, like IgnorePaging = "true", and enabled the commandItemDisplay for the masterViewTable and for both detailTables.

    The export of the Master works as expected, only the master table data ingnoring paging. However when pressing one of the export buttons on one of the two detail grids, the settings are ignored and the export only contains the visible records of the detailpage. This is the code (there is no code behind for the export).

    <telerik:RadGrid GridTableView="true" ID="grdBuyer" runat="server" Visible="false"
                AutoGenerateColumns="false"
                AllowPaging="true"
                PageSize="10"
                AllowSorting="true"
     
                PagerStyle-AlwaysVisible="true"
     
                OnNeedDataSource="grdBuyer_NeedDataSource"
                OnItemCommand="grdGeneral_ItemCommand"
                OnDetailTableDataBind="grdBuyer_DetailTableDataBind"
              >
      
                <ExportSettings IgnorePaging="true" OpenInNewWindow="true" ExportOnlyData="true" HideStructureColumns="true" />
                <MasterTableView DataKeyNames="buyer_member_id" CommandItemDisplay="Top">
                  <CommandItemSettings ShowExportToWordButton="true" ShowExportToExcelButton="true" ShowExportToCsvButton="true" ShowExportToPdfButton="true" ShowAddNewRecordButton="false" ShowRefreshButton="false"/>
                  <Columns>
                    <telerik:GridBoundColumn DataField="buyer_member" HeaderText="Buyer" />
                    <telerik:GridButtonColumn DataTextField="revenue" HeaderText="Revenue" CommandName="show_stats_rev" DataTextFormatString="$ {0:n2}">
                      <ItemStyle Width="150" />
                    </telerik:GridButtonColumn>
                    <telerik:GridButtonColumn DataTextField="imps" HeaderText="Impressions" CommandName="show_stats_imps" DataTextFormatString="{0:n0}">
                      <ItemStyle Width="150" />
                    </telerik:GridButtonColumn>
                    <telerik:GridButtonColumn DataTextField="ecpm" HeaderText="eCPM" CommandName="show_stats_ecpm" DataTextFormatString="$ {0:n2}">
                      <ItemStyle Width="100" />
                    </telerik:GridButtonColumn>
                  </Columns>
                   
                  <DetailTables>
                    <telerik:GridTableView DataKeyNames="buyer_member_id,country" Name="country" PageSize="5" CommandItemDisplay="Top" CanRetrieveAllData="true">
                      <CommandItemSettings ShowExportToWordButton="true" ShowExportToExcelButton="true" ShowExportToCsvButton="true" ShowExportToPdfButton="true" ShowAddNewRecordButton="false" ShowRefreshButton="false"/>
                      <Columns>
                        <telerik:GridBoundColumn DataField="name" HeaderText="Country" />
                        <telerik:GridButtonColumn DataTextField="revenue" HeaderText="Revenue" CommandName="show_stats_country_rev" DataTextFormatString="$ {0:n2}">
                          <ItemStyle Width="150" />
                        </telerik:GridButtonColumn>
                        <telerik:GridButtonColumn DataTextField="imps" HeaderText="Impressions" CommandName="show_stats_country_imps" DataTextFormatString="{0:n0}">
                          <ItemStyle Width="150" />
                        </telerik:GridButtonColumn>
                        <telerik:GridButtonColumn DataTextField="ecpm" HeaderText="eCPM" CommandName="show_stats_country_ecpm" DataTextFormatString="$ {0:n2}">
                          <ItemStyle Width="100" />
                        </telerik:GridButtonColumn>
                      </Columns>
                      <DetailTables>
                        <telerik:GridTableView DataKeyNames="buyer_member_id,country,size" Name="size" PageSize="5" CommandItemDisplay="Top">
                          <CommandItemSettings ShowExportToWordButton="true" ShowExportToExcelButton="true" ShowExportToCsvButton="true" ShowExportToPdfButton="true" ShowAddNewRecordButton="false" ShowRefreshButton="false"/>
                          <Columns>
                            <telerik:GridBoundColumn DataField="size" HeaderText="Size" /> 
                            <telerik:GridButtonColumn DataTextField="revenue" HeaderText="Revenue" CommandName="show_stats_country_size_rev" DataTextFormatString="$ {0:n2}">
                              <ItemStyle Width="150" />
                            </telerik:GridButtonColumn>
                            <telerik:GridButtonColumn DataTextField="imps" HeaderText="Impressions" CommandName="show_stats_country_size_imps" DataTextFormatString="{0:n0}">
                              <ItemStyle Width="150" />
                            </telerik:GridButtonColumn>
                            <telerik:GridButtonColumn DataTextField="ecpm" HeaderText="eCPM" CommandName="show_stats_country_size_ecpm" DataTextFormatString="$ {0:n2}">
                              <ItemStyle Width="100" />
                            </telerik:GridButtonColumn>
                          </Columns>
                        </telerik:GridTableView>
                      </DetailTables>
                    </telerik:GridTableView>
                  </DetailTables>
     
                </MasterTableView>
              </telerik:RadGrid>

    Could someone please help me how to use these export settings for the detailtables?

    Thanks,
    Danny

  2. Jayesh Goyani
    Jayesh Goyani avatar
    2665 posts
    Member since:
    May 2010

    Posted 03 Feb 2012 Link to this post

    Hello Danny,

    please check below code snippet.

    <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource"
                AllowFilteringByColumn="true"
     
    OnDetailTableDataBind="RadGrid1_DetailTableDataBind"
                AllowPaging="true" PageSize="2">
                <ExportSettings IgnorePaging="true" HideStructureColumns="true" OpenInNewWindow="true" ExportOnlyData="true">
                </ExportSettings>
                <MasterTableView DataKeyNames="ID" CommandItemDisplay="Top">
                    <CommandItemSettings ShowExportToWordButton="true" ShowExportToExcelButton="true"
                        ShowExportToCsvButton="true" ShowExportToPdfButton="true" ShowAddNewRecordButton="false"
                        ShowRefreshButton="false" />
                    <Columns>
                        <telerik:GridBoundColumn DataField="ID" HeaderText="ID" UniqueName="ID">
                        </telerik:GridBoundColumn>
                        <telerik:GridTemplateColumn UniqueName="Name">
                            <ItemTemplate>
                                <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtName" runat="server" Text='<%# Eval("Name") %>'></asp:TextBox>
                            </EditItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridEditCommandColumn>
                        </telerik:GridEditCommandColumn>
                    </Columns>
                    <DetailTables>
                        <telerik:GridTableView>
                            <Columns>
                                <telerik:GridBoundColumn DataField="ID" HeaderText="ID" UniqueName="ID">
                                </telerik:GridBoundColumn>
                            </Columns>
                            <DetailTables>
                                <telerik:GridTableView>
                                    <Columns>
                                        <telerik:GridBoundColumn DataField="ID" HeaderText="ID" UniqueName="ID">
                                        </telerik:GridBoundColumn>
                                    </Columns>
                                </telerik:GridTableView>
                            </DetailTables>
                        </telerik:GridTableView>
                    </DetailTables>
                </MasterTableView>
                 
            </telerik:RadGrid>
    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
            {
                dynamic data = new[] {
                    new { ID = 1, Name ="Name1",Isbool = true},
                    new { ID = 2, Name = "Name1",Isbool = true},
                    new { ID = 3, Name = "Name2",Isbool = false},
                    new { ID = 4, Name = "Name2",Isbool = false},
                    new { ID = 5, Name = "Name3",Isbool = false}
                };
                RadGrid1.DataSource = data;
            }
     
    protected void RadGrid1_DetailTableDataBind(object sender, GridDetailTableDataBindEventArgs e)
            {
                dynamic data = new[] {
                    new { ID = 1, Name ="Name1",Isbool = true},
                    new { ID = 2, Name = "Name1",Isbool = true},
                    new { ID = 3, Name = "Name2",Isbool = false},
                    new { ID = 4, Name = "Name2",Isbool = false},
                    new { ID = 5, Name = "Name3",Isbool = false}
                };
     
                e.DetailTableView.DataSource = data;
            }
     
    protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
            {
                if (e.CommandName == RadGrid.ExportToPdfCommandName)
                {
                    RadGrid1.MasterTableView.HierarchyDefaultExpanded = true;
                    RadGrid1.MasterTableView.DetailTables[0].HierarchyDefaultExpanded = true;
                }
    }


    for more info check this link.

    Thanks,
    Jayesh Goyani

  3. DevCraft Release
  4. Danny
    Danny avatar
    29 posts
    Member since:
    Feb 2012

    Posted 07 Feb 2012 Link to this post

    Hi Jayesh,

    Thanks for your reply.

    The only difference in you code that I could find was this part:

    RadGrid1.MasterTableView.HierarchyDefaultExpanded = true;
    RadGrid1.MasterTableView.DetailTables[0].HierarchyDefaultExpanded = true;

    However as soon as I set these options in my code I don't receive any export file anymore. When debugging the itemCommand event doesn't throw any errors, but the browser just doesn't output anything. 

    So I changed my itemcommand to with this code (also tried setting it for the detail tables with the same effect):

    protected void grdGeneral_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e) {
      try {
        if (e.CommandName == RadGrid.ExportToExcelCommandName ||
            e.CommandName == RadGrid.ExportToCsvCommandName ||
            e.CommandName == RadGrid.ExportToPdfCommandName ||
            e.CommandName == RadGrid.ExportToWordCommandName
            ) {
     
              grdBuyer.MasterTableView.HierarchyDefaultExpanded = true;
                   
              //I also show hide some columns here for export
        }
     
             
      }
      catch (Exception ex) {
        Exceptions.Process(ex);
      }
    }

    But the HierarchyDefaultExpanded seems to prevent the browser from sending any of the export files without throwing an actually error. Any idea on how to locate / fix this problem?

    Thanks,
    Danny

  5. Danny
    Danny avatar
    29 posts
    Member since:
    Feb 2012

    Posted 07 Feb 2012 Link to this post

    I suddenly did get an export file, it just took forever because it showing the data from every level. So a combined export of the master with his details and the details of the details.

    This however is not what I want to export, I want it like this.

    When clicking export on the mastertable, export all records from the mastertable.
    When clicking export in the detail table, export all records of the detail table (for that master table record).
    When clicking export in the detail of the detail table, export all records of the detail detail table (for that detail table record).

    I've set these options through the export settings on the master table, but they don't seem to be used by the detail tables.

    Thanks,
    Danny

  6. Danny
    Danny avatar
    29 posts
    Member since:
    Feb 2012

    Posted 07 Feb 2012 Link to this post

    While still breaking my head over this problem I found a demo with NestedViewTemplates. Thinking maybe this what I needed I've rewritten my code to use these instead of the detailtables. However now when exporting the top level, it also calls the NeedDataSource of the nestedviewtemplates. I need it to just simply export it's own records, nothing more. Is there a setting for the nestedViewTemplates not to load / be included in exports?

    Thanks,
    Danny

  7. Mira
    Admin
    Mira avatar
    1124 posts
    Member since:
    Sep 2012

    Posted 10 Feb 2012 Link to this post

    Hello,

    You can see how hierarchical RadGrid can be exported here.
    However, I am afraid that export of detail tables only is not supported.

    I hope this helps.

    Regards,
    Mira
    the Telerik team

    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>

  8. Danny
    Danny avatar
    29 posts
    Member since:
    Feb 2012

    Posted 28 Feb 2012 Link to this post

    Hi Mira,

    It's been a while, but I've been actually able to solve the problems by calling the method below in the itemCommand when exporting:

    /// <summary>
    /// Prepare a grid for export
    /// </summary>
    /// <param name="grid">The radgrid to prepare</param>
    private void _fixExportGrid(object grid){
      //First level, the grid itself
      if (grid is RadGrid) {
        //Disable paging and the the expand collapse column
        (grid as RadGrid).ExportSettings.IgnorePaging = true;
        (grid as RadGrid).MasterTableView.ExpandCollapseColumn.Visible = false;
        //Show / hide the correct columns for the master table view
        (grid as RadGrid).MasterTableView.GetColumn("RevenueDisplay").Display = false;
        (grid as RadGrid).MasterTableView.GetColumn("RevenueExport").Display = true;
     
        if ((grid as RadGrid).MasterTableView.HasDetailTables) {
          // for each master record, check if there are child items and if so loop through and hide the column
          foreach (GridDataItem item in (grid as RadGrid).MasterTableView.Items) {
            if (item.HasChildItems) {
              //To speed it up, only handle expanded children, as collapsed can't be exported
              if (item.Expanded) {
                //Fix all children recursivly
                foreach (GridTableView innerDetailView in item.ChildItem.NestedTableViews) {
                  _fixExportGrid(innerDetailView);
                }
              }
            }
          }
        }
      }
     
      //Deeper level, a detail grid
      if (grid is GridTableView) {
        //Start by showing the correct columjns
        (grid as GridTableView).GetColumn("RevenueDisplay").Display = false;
        (grid as GridTableView).GetColumn("RevenueExport").Display = true;
     
        //Handle all children recursivly, when their expaned
        foreach (GridDataItem item in (grid as GridTableView).Items) {
          if (item.Expanded) {
            foreach (GridTableView innerDetailView in item.ChildItem.NestedTableViews) {
              _fixExportGrid(innerDetailView);
            }
          }
        }
     
        //Disable paging for all records in export, hide the first column and rebind the data for applying
        //But only after showing / hiding the columns on all levels, or else their missed!
        (grid as GridTableView).AllowPaging = false;
        (grid as GridTableView).ExpandCollapseColumn.Display = false;
        (grid as GridTableView).Rebind();
      }
    }

    I don't know if this should be the way to do it, but it works and doesn't seem to cause any other problems.

    Regards,
    Danny

  9. Amod
    Amod avatar
    2 posts
    Member since:
    Aug 2012

    Posted 16 Aug 2012 Link to this post

    Hi,

    I used this method.it is exporting GridTableView  name as "country" but after expanded country row GridTableView  name "size" is not exporting.i used this (grid as GridTableView).HierarchyDefaultExpanded=true;  inside //Handle all children recursivly, when their expaned 
    then it is exported but all the child records.i want export only which i expanded.


    //Deeper level, a detail grid
      if (grid is GridTableView) {
        //Start by showing the correct columjns
        (grid as GridTableView).GetColumn("RevenueDisplay").Display = false;
        (grid as GridTableView).GetColumn("RevenueExport").Display = true;
     
        //Handle all children recursivly, when their expaned
        foreach (GridDataItem item in (grid as GridTableView).Items) {
          if (item.Expanded) {
            foreach (GridTableView innerDetailView in item.ChildItem.NestedTableViews) {
              _fixExportGrid(innerDetailView);
    Added ---------(grid as GridTableView).HierarchyDefaultExpanded=true; 
            }
          }
        }


  10. Daniel
    Admin
    Daniel avatar
    4780 posts
    Member since:
    Sep 2012

    Posted 21 Aug 2012 Link to this post

    Hello Amod,

    Test the attached demo and see if it helps.

    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.

  11. Amod
    Amod avatar
    2 posts
    Member since:
    Aug 2012

    Posted 11 Sep 2012 Link to this post

     Hi i am not able to attach zip file so i am pasting code here,please check this code and please help me out this.

    it is not doing allowpaging = flase for "tblchild2" and for  "tblchild2"  expanded row should be exported with all "tblchild1" records but it is exporting all rows "tblchild2" records.

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" enableEventValidation ="false" %><
    br>
    <br>
    <br>
    <%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI, Version=2012.1.411.35, Culture=neutral, PublicKeyToken=121fae78165ba3d4" %><br>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><br>
    <html xmlns="http://www.w3.org/1999/xhtml"><br>
    <head runat="server"><br>
        <title></title><br>
    </head><br>
    <body><br>
        <form id="form1" runat="server"><br>
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server"><br>
            <Scripts><br>
                <%--Needed for JavaScript IntelliSense in VS2010--%><br>
                <%--For VS2008 replace RadScriptManager with ScriptManager--%><br>
                <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js" /><br>
                <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js" /><br>
                <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQueryInclude.js" /><br>
            </Scripts><br>
        </telerik:RadScriptManager><br>
    <br>
    <br>
        <script type="text/javascript"><br>
            //Put your JavaScript code here.<br>
        </script><br>
    <br>
    <br>
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server"><br>
        </telerik:RadAjaxManager><br>
        <div><br>
            <telerik:RadGrid GridLines="None" ID="grdDisbursementDetail" Skin="Outlook" runat="server"<br>
                AllowPaging="True" ShowStatusBar="true" AllowSorting="True" AutoGenerateColumns="False"<br>
                AllowMultiRowSelection="True" PageSize="5" EnableLinqExpressions="false" ClientSettings-AllowColumnsReorder="false"<br>
                EnableEmbeddedSkins="true" ForeColor="Black" OnItemCommand="grdDisbursementDetail_ItemCommand"<br>
                CellPadding="0" MasterTableView-CellPadding="0" ItemStyle-BorderWidth="1px" ItemStyle-BorderColor="#095b79"<br>
                OnNeedDataSource="grdDisbursementDetail_NeedDataSource" OnDetailTableDataBind="grdDisbursementDetail_DetailTableDataBind"<br>
                OnSelectedIndexChanged="grdDisbursementDetail_SelectedIndexChanged"><br>
                <%--DataKeyNames="EntityTypeName"--%><br>
                <HeaderContextMenu EnableEmbeddedSkins="True"><br>
                </HeaderContextMenu><br>
                <MasterTableView Width="99%" AllowMultiColumnSorting="True"<br>
                    AllowCustomPaging="true" AllowNaturalSort="false" AutoGenerateColumns="False"><br>
                    <PagerStyle Visible="true" Mode="NextPrevAndNumeric" /><br>
                    <CommandItemSettings ExportToPdfText="Export to PDF" /><br>
                    <RowIndicatorColumn Visible="False"><br>
                    </RowIndicatorColumn><br>
                    <DetailTables><br>
                        <telerik:GridTableView Name="tblchild1" runat="server" Width="100%"<br>
                            AutoGenerateColumns="false" BorderColor="red" ExpandCollapseColumn-ShowSortIcon="true"><br>
                            <DetailTables><br>
                                <telerik:GridTableView Name="tblchild2" runat="server" Width="100%" <br>
                                    AutoGenerateColumns="false" BorderColor="Aqua"><br>
                                    <CommandItemSettings ExportToPdfText="Export to PDF" /><br>
                                    <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column" Visible="True"><br>
                                    </RowIndicatorColumn><br>
                                    <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column" Visible="True"><br>
                                    </ExpandCollapseColumn><br>
                                    <Columns><br>
                                        <telerik:GridBoundColumn HeaderText="PaymentRefNo" DataField="PaymentRefNo" DataType="System.Int32"<br>
                                            HeaderStyle-Wrap="true" ItemStyle-CssClass="alignCenter gridCellPadding columnWidth"<br>
                                            ItemStyle-HorizontalAlign="Center" ItemStyle-Width="30%" Visible="false"><br>
                                        </telerik:GridBoundColumn><br>
                                        <telerik:GridBoundColumn HeaderText="EmployerId" DataField="EmployerId" DataType="System.Int32"<br>
                                            HeaderStyle-Wrap="true" ItemStyle-CssClass="alignCenter gridCellPadding columnWidth"<br>
                                            ItemStyle-Width="30%" ItemStyle-HorizontalAlign="Center"><br>
                                        </telerik:GridBoundColumn><br>
                                        <telerik:GridBoundColumn HeaderText="Master Cont ID" DataField="MasterContID" DataType="System.String"<br>
                                            HeaderStyle-Wrap="true" ItemStyle-CssClass="alignCenter gridCellPadding columnWidth"<br>
                                            ItemStyle-Width="30%" ItemStyle-HorizontalAlign="Center"><br>
                                        </telerik:GridBoundColumn><br>
                                        <telerik:GridBoundColumn HeaderText="EmployerName" DataField="EmployerName" DataType="System.String"<br>
                                            HeaderStyle-Wrap="true" ItemStyle-CssClass="alignCenter gridCellPadding columnWidth"<br>
                                            ItemStyle-HorizontalAlign="Center" ItemStyle-Width="30%"><br>
                                        </telerik:GridBoundColumn><br>
                                        <telerik:GridBoundColumn DataField="EmpAmount" HeaderText="EmpAmount" DataType="System.Int32"<br>
                                            DataFormatString="{0:$###,##0.00}" HeaderStyle-Wrap="true" ItemStyle-CssClass="alignCenter gridCellPadding"<br>
                                            ItemStyle-HorizontalAlign="Center" ItemStyle-Width="30%"><br>
                                        </telerik:GridBoundColumn><br>
                                    </Columns><br>
                                </telerik:GridTableView><br>
                            </DetailTables><br>
                            <Columns><br>
                                <telerik:GridBoundColumn HeaderText="FindingElementLevelOne" DataField="FindingElementLevelOne"<br>
                                    DataType="System.String" HeaderStyle-Wrap="true" ItemStyle-CssClass="alignCenter gridCellPadding columnWidth"<br>
                                    ItemStyle-HorizontalAlign="Center" ItemStyle-Width="30%" Visible="false"><br>
                                </telerik:GridBoundColumn><br>
                                <telerik:GridBoundColumn HeaderText="PayOutId" DataField="PayOutId" DataType="System.Int32"<br>
                                    HeaderStyle-Wrap="true" ItemStyle-CssClass="alignCenter gridCellPadding columnWidth"<br>
                                    ItemStyle-Width="30%" ItemStyle-HorizontalAlign="Center" Visible="true"><br>
                                </telerik:GridBoundColumn><br>
                                <telerik:GridBoundColumn HeaderText="DisbursementDate" DataField="DisbursementDate"<br>
                                    DataFormatString="{0:dd/MM/yyyy}" DataType="System.DateTime" HeaderStyle-Wrap="true"<br>
                                    ItemStyle-CssClass="alignCenter gridCellPadding columnWidth" ItemStyle-Width="30%"<br>
                                    ItemStyle-HorizontalAlign="Center"><br>
                                </telerik:GridBoundColumn><br>
                                <telerik:GridBoundColumn HeaderText="Transaction ID" DataField="PaymentRefNo" DataType="System.Int32"<br>
                                    HeaderStyle-Wrap="true" ItemStyle-CssClass="alignCenter gridCellPadding columnWidth"<br>
                                    ItemStyle-HorizontalAlign="Center" ItemStyle-Width="30%"><br>
                                </telerik:GridBoundColumn><br>
                                <telerik:GridBoundColumn DataField="EmployerID" HeaderText="EmployerId" DataType="System.Int32"<br>
                                    HeaderStyle-Wrap="true" ItemStyle-CssClass="alignCenter gridCellPadding" ItemStyle-HorizontalAlign="Center"<br>
                                    ItemStyle-Width="30%"><br>
                                </telerik:GridBoundColumn><br>
                                <%--  <telerik:GridBoundColumn DataField="TotalContributionAmount" HeaderText="TotalContributionAmount" DataType="System.Double"<br>
                                HeaderStyle-Wrap="true" ItemStyle-CssClass="alignCenter gridCellPadding" ItemStyle-HorizontalAlign="Center"<br>
                                ItemStyle-Width="30%"><br>
                            </telerik:GridBoundColumn>--%><br>
                                <%-- <telerik:GridBoundColumn DataField="MasterContributionId" HeaderText="MasterContribId"<br>
                                DataType="System.String" HeaderStyle-Wrap="true" ItemStyle-CssClass="alignCenter gridCellPadding"<br>
                                ItemStyle-HorizontalAlign="Center" ItemStyle-Width="30%"><br>
                            </telerik:GridBoundColumn>--%><br>
                                <telerik:GridBoundColumn DataField="EntityType" HeaderText="EntityType" DataType="System.String"<br>
                                    HeaderStyle-Wrap="true" ItemStyle-CssClass="alignCenter gridCellPadding" ItemStyle-HorizontalAlign="Center"<br>
                                    ItemStyle-Width="30%"><br>
                                </telerik:GridBoundColumn><br>
                                <telerik:GridBoundColumn DataField="EntityName" HeaderText="EntityName" DataType="System.String"<br>
                                    HeaderStyle-Wrap="true" ItemStyle-CssClass="alignCenter gridCellPadding" ItemStyle-HorizontalAlign="Center"<br>
                                    ItemStyle-Width="30%"><br>
                                </telerik:GridBoundColumn><br>
                                <telerik:GridBoundColumn DataField="SPIN" DataType="System.String" HeaderText="SpinId"<br>
                                    HeaderStyle-Wrap="true" ItemStyle-CssClass="alignCenter gridCellPadding" ItemStyle-HorizontalAlign="Center"<br>
                                    ItemStyle-Width="30%"><br>
                                </telerik:GridBoundColumn><br>
                                <telerik:GridBoundColumn DataField="Amount" DataType="System.Double" HeaderText="Amount"<br>
                                    DataFormatString="{0:C}" HeaderStyle-Wrap="true" ItemStyle-CssClass="alignCenter gridCellPadding"<br>
                                    ItemStyle-HorizontalAlign="Center" ItemStyle-Width="30%"><br>
                                </telerik:GridBoundColumn><br>
                            </Columns><br>
                        </telerik:GridTableView><br>
                    </DetailTables><br>
                    <Columns><br>
                        <telerik:GridBoundColumn DataField="EntityTypeName" HeaderText="" ItemStyle-Width="100%"<br>
                            UniqueName="colmEntityTypeName" HeaderStyle-Width="10%" AllowSorting="true" DataType="System.String"<br>
                            HeaderStyle-Wrap="false" ItemStyle-CssClass="gridCellPadding" ItemStyle-HorizontalAlign="Left"><br>
                        </telerik:GridBoundColumn><br>
                    </Columns><br>
                </MasterTableView><br>
                <ClientSettings AllowColumnsReorder="False"><br>
                    <Scrolling AllowScroll="False" UseStaticHeaders="True" /><br>
                    <Selecting AllowRowSelect="True" /><br>
                </ClientSettings><br>
            </telerik:RadGrid><br>
        </div><br>
        <br>
        <div><br>
        <asp:Button ID="Button1" runat="server" Text="Exoport to PDF" <br>
                onclick="Button1_Click"/><br>
        </div><br>
        </form><br>
    </body><br>
    </html><br>
    <div><br>
    <br>
    ////////////////////////////.cs code//////////////////////////////////////////////////////////////////////////<br>
    <br>
    using System;<br>
    using System.Collections.Generic;<br>
    using System.Linq;<br>
    using System.Web;<br>
    using System.Web.UI;<br>
    using System.Web.UI.WebControls;<br>
    using DataClassLibrary;<br>
    using System.Data;<br>
    using Telerik.Web.UI;<br>
    using System.Collections;<br>
    using System.IO;<br>
    using System.Configuration;<br>
    using System.Web.Security;<br>
    using System.Web.UI.WebControls.WebParts;<br>
    using System.Web.UI.HtmlControls;<br>
    public partial class Default2 : System.Web.UI.Page<br>
    {<br>
        <br>
        List<int> expandedItems = new List<int>();   <br>
       <br>
        bool chkExpandCollapse = false;<br>
        private bool isDetailedViewCalled { get; set; }<br>
        protected void Page_Load(object sender, EventArgs e)<br>
        {<br>
            if (!IsPostBack)<br>
            {<br>
                grdDisbursementDetail.DataSource = DtOne();<br>
                <br>
            }<br>
        }<br>
    <br>
    <br>
        protected void grdDisbursementDetail_NeedDataSource(object source, GridNeedDataSourceEventArgs e)<br>
        {<br>
    <br>
    <br>
        }<br>
    <br>
    <br>
        protected void grdDisbursementDetail_DetailTableDataBind(object source, GridDetailTableDataBindEventArgs e)<br>
        {<br>
            chkExpandCollapse = true;<br>
                GridDataItem dataItem = (GridDataItem)e.DetailTableView.ParentItem;<br>
    <br>
    <br>
                switch (e.DetailTableView.Name)<br>
                {<br>
                    case "tblchild1":<br>
                        {<br>
                            string entityType = "AAS funds";<br>
                            if (entityType == "AAS funds")<br>
                            {<br>
                                e.DetailTableView.Columns[1].Visible = false;<br>
                                e.DetailTableView.Columns[4].Visible = false;<br>
                            }<br>
                            else if (entityType == "Employer")<br>
                            {<br>
                                e.DetailTableView.Columns[1].Visible = false;<br>
                                e.DetailTableView.Columns[3].Visible = false;<br>
                                e.DetailTableView.Columns[5].Visible = false;<br>
                                e.DetailTableView.Columns[6].Visible = false;<br>
                                e.DetailTableView.Columns[7].Visible = false;<br>
                            }<br>
                            else if (entityType == "QuickSuper")<br>
                            {<br>
                                e.DetailTableView.Columns[4].Visible = false;<br>
                            }<br>
                            else if (entityType == "Fees")<br>
                            {<br>
                                e.DetailTableView.Columns[1].Visible = false;<br>
                                e.DetailTableView.Columns[4].Visible = false;<br>
                                e.DetailTableView.Columns[5].Visible = false;<br>
                                e.DetailTableView.Columns[6].Visible = false;<br>
                                e.DetailTableView.Columns[7].Visible = false;<br>
                            }<br>
                            e.DetailTableView.DataSource = DtTwo();<br>
                           <br>
                            break;<br>
                        }<br>
    <br>
    <br>
                    case "tblchild2":<br>
                        {<br>
                            //string payOutId= e.DetailTableView.Columns.FindByDataField("PayOutId").ToString();<br>
                            if (e.DetailTableView.ParentItem.Cells[2].Text.ToLower() == "employerlevelone")<br>
                            {<br>
                                e.DetailTableView.Columns[1].Visible = false;<br>
                            }<br>
                            if (e.DetailTableView.ParentItem.Cells[2].Text.ToLower() == "aas fundslevelone")<br>
                            {<br>
                                e.DetailTableView.Columns[2].Visible = false;<br>
                            }<br>
                            if (e.DetailTableView.ParentItem.Cells[2].Text.ToLower() == "quicksuperlevelone")<br>
                            {<br>
                                e.DetailTableView.Columns[2].Visible = false;<br>
                            }<br>
                            if (e.DetailTableView.ParentItem.Cells[2].Text.ToLower() == "feeslevelone")<br>
                            {<br>
                                e.DetailTableView.Columns[2].Visible = false;<br>
                            }<br>
                            e.DetailTableView.DataSource = DtThree();                       <br>
                            break;<br>
                        }<br>
                }<br>
           <br>
        }<br>
    <br>
    <br>
        protected void grdDisbursementDetail_ItemCommand(object source, GridCommandEventArgs e)<br>
        {<br>
          <br>
        }<br>
    <br>
    <br>
        protected void grdDisbursementDetail_SelectedIndexChanged(object sender, EventArgs e)<br>
        {<br>
        } <br>
    <br>
    <br>
        private DataTable DtOne()<br>
        {<br>
            DataTable dtOne = new DataTable();<br>
            dtOne.Columns.Add("EntityTypeName", typeof(string));<br>
            dtOne.Columns.Add("EntityTypeId", typeof(string));<br>
            dtOne.Rows.Add("AAS Funds", "AAS Funds");<br>
    <br>
    <br>
            return dtOne;<br>
        }<br>
    <br>
    <br>
        private DataTable DtTwo()<br>
        {<br>
            DataTable dtTwo = new DataTable();<br>
            dtTwo.Columns.Add("FindingElementLevelOne", typeof(string));<br>
            dtTwo.Columns.Add("PayOutId", typeof(Int32));<br>
            dtTwo.Columns.Add("DisbursementDate", typeof(Int32));<br>
            dtTwo.Columns.Add("PaymentRefNo", typeof(Int32));<br>
            dtTwo.Columns.Add("EmployerId", typeof(Int32));<br>
            dtTwo.Columns.Add("EntityType", typeof(string));<br>
            dtTwo.Columns.Add("EntityName", typeof(string));<br>
            dtTwo.Columns.Add("SpinId", typeof(string));<br>
            dtTwo.Columns.Add("Amount", typeof(double));<br>
            for (int i = 0; i < 10; i++)<br>
                dtTwo.Rows.Add("test", i, i, 1, 1, "AAS Funds", "ASSET Super" + i, "ASS0100AU",10.35);        <br>
            return dtTwo;<br>
        }<br>
    <br>
    <br>
        private DataTable DtThree()<br>
        {<br>
            DataTable dtThree = new DataTable();<br>
    <br>
    <br>
            dtThree.Columns.Add("PaymentRefNo", typeof(Int32));<br>
            dtThree.Columns.Add("EmployerId", typeof(Int32));<br>
            dtThree.Columns.Add("MasterContID", typeof(string));<br>
            dtThree.Columns.Add("EmployerName", typeof(string));<br>
            dtThree.Columns.Add("EmpAmount", typeof(Int32));<br>
            for (int i = 0; i < 10; i++)<br>
                dtThree.Rows.Add(1, i, "Test" + i, "Test", 10 + 1);<br>
            return dtThree;<br>
        }<br>
        <br>
        /// <summary><br>
        /// Prepare a grid for export<br>
        /// </summary><br>
        /// <param name="grid">The radgrid to prepare</param><br>
        private void _fixExportGrid(object grid)<br>
        {<br>
           <br>
            if (grid is RadGrid)<br>
            {<br>
                if ((grid as RadGrid).MasterTableView.HasDetailTables)<br>
                {<br>
                    foreach (GridDataItem item in (grid as RadGrid).MasterTableView.Items)<br>
                    {<br>
                        if (item.Expanded)<br>
                        {<br>
                            foreach (GridTableView innerDetailView in item.ChildItem.NestedTableViews)<br>
                            {<br>
                               <br>
                                gridTable(innerDetailView); <br>
                               <br>
                            }<br>
                        }<br>
                    }           <br>
                }<br>
            }             <br>
            <br>
        }<br>
    <br>
    <br>
        private void gridTable(object gridTable)<br>
        {<br>
            if (gridTable is GridTableView)<br>
            {<br>
                foreach (GridDataItem item in (gridTable as GridTableView).Items)<br>
                {<br>
                    Boolean a = item.Expanded;<br>
                    if (item.Expanded)<br>
                    {<br>
                        foreach (GridTableView innerDetailView in item.ChildItem.NestedTableViews)<br>
                        {<br>
                           <br>
                            (gridTable as GridTableView).HierarchyDefaultExpanded = true;<br>
                        }<br>
                    } <br>
                }<br>
                (gridTable as GridTableView).AllowPaging = false;<br>
                (gridTable as GridTableView).Rebind(); <br>
               <br>
            }<br>
        }  <br>
    <br>
    <br>
        protected void Button1_Click(object sender, EventArgs e)<br>
        {<br>
            <br>
            grdDisbursementDetail.Page.Response.ClearHeaders();<br>
            grdDisbursementDetail.Page.Response.Cache.SetCacheability(HttpCacheability.Private);<br>
            grdDisbursementDetail.ExportSettings.ExportOnlyData = true;<br>
            grdDisbursementDetail.ExportSettings.OpenInNewWindow = true;<br>
            grdDisbursementDetail.GridLines = GridLines.Both;<br>
           <br>
           <br>
           <br>
            _fixExportGrid(grdDisbursementDetail);          <br>
            grdDisbursementDetail.MasterTableView.ExportToPdf();<br>
           <br>
          <br>
        }<br>
    }<br>
    <br>
    <br>
        <br>
      <br>
        <br>
    <br>
    <br>
    <br>
    <br>
    <br></div>

  12. Daniel
    Admin
    Daniel avatar
    4780 posts
    Member since:
    Sep 2012

    Posted 14 Sep 2012 Link to this post

    Hello Amod,

    I modified your code and put it in a runnable demo that is attached to this code. Please have a look into the changes and then let me know if you need further assistance.
    I have optimized the code that expands the detail tables and also I have moved the code that binds the master table from the Page_Load event to the NeedDataSource event. This is needed because you are mixing two incompatible approaches - simple and advanced data-binding.

    Best 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.

  13. Kalpna
    Kalpna avatar
    37 posts
    Member since:
    Jun 2013

    Posted 07 Apr Link to this post

    Hello all,
    I have a problem in radgrid export to pdf. I have a master table with 4 detail tables. Some detail tables contain data and some not.
    If i export to pdf the radgrid data then it will export all data(including detail table which have no data and shows No child records to display)  
    I want that when i will export the data of pdf then pdf will will show only those detail tables which contain data. and hide those which shows 'No child records to display.'
    This is my code for export to pdf:
    rgdQumaiqs.MasterTableView.HierarchyDefaultExpanded
    = true;

                rgd.ExportSettings.FileName
    = ddlscheme.SelectedItem.Text;

                rgd.GridLines
    = GridLines.None;

                GridColumn Text0 = rgd.MasterTableView.GetColumn("Text0");

               
    Text0.HeaderStyle.Width = Unit.Pixel(500);

                if (rgd.MasterTableView.HasDetailTables)

                {

                   
    foreach (GridTableView
    gridTableView in rgd.MasterTableView.DetailTables)

                   
    {

                     


                       
    gridTableView.HierarchyDefaultExpanded = true;

                       
    gridTableView.GetColumn("Numbering1").HeaderStyle.Width
    = Unit.Pixel(100);

                       
    gridTableView.GetColumn("Text1").HeaderStyle.Width
    = Unit.Pixel(420);

                       
    if (gridTableView.HasDetailTables)

                       
    {

                            foreach (GridTableView
    gridTableView2 in gridTableView.DetailTables)

                            {

                               
    gridTableView2.HierarchyDefaultExpanded = true;

                               
    gridTableView2.GetColumn("Text2").HeaderStyle.Width
    = Unit.Pixel(380);

                               
    gridTableView2.GetColumn("Numbering2").HeaderStyle.Width
    = Unit.Pixel(100);

                                if (gridTableView2.HasDetailTables)

                                {

                                    foreach (GridTableView
    gridTableView3 in gridTableView2.DetailTables)

                                    {

                                        gridTableView3.GetColumn("Text3").HeaderStyle.Width = Unit.Pixel(340);

                                       
    gridTableView3.GetColumn("Numbering3").HeaderStyle.Width
    = Unit.Pixel(100);

                                        if (gridTableView3.NoDetailRecordsText != "No child records to display.")

                                        {

                                            if (gridTableView3.HasDetailTables)

                                            {

                                                foreach (GridTableView
    gridTableView4 in gridTableView3.DetailTables)

                                                {

                                                   
    gridTableView4.GetColumn("Text4").HeaderStyle.Width
    = Unit.Pixel(300);

                                                   
    gridTableView4.GetColumn("Numbering4").HeaderStyle.Width
    = Unit.Pixel(100);

                                                }

                                            }

                                        }

                                        else

                                        {

                                           
    gridTableView3.HierarchyDefaultExpanded = false;

                                        }

                    
                   }

                                }

                            }

                       
    }

                   
    }

                }

                rgd.Rebind();

                rgd.ExportSettings.OpenInNewWindow
    = true;

                rgd.ExportSettings.Pdf.PageBottomMargin
    = 15;

                rgd.ExportSettings.Pdf.PageTopMargin
    = 15;

                rgd.ExportSettings.Pdf.PageLeftMargin
    = 15;

                rgd.ExportSettings.Pdf.PageRightMargin
    = 15;

                rgd.MasterTableView.ExportToPdf();

     And the following attchment is wrong result which i do'nt want. I dont want to show those detail table which does not have any data. Please help me to solve this problem. It is very urgent for me.


























  14. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 07 Apr in reply to Kalpna Link to this post

    Hi Kalpna,

    You can try the approach mentioned in this documentation on Hiding the expand/collapse images when no records. You can do this on the Export button click event. Please try and let me know if any concern.

    Thanks,
    Shinu

Back to Top
DevCraft Release