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

Export Grouped Data

1 Answer 81 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Princy
Top achievements
Rank 2
Princy asked on 06 Mar 2009, 01:05 PM

Requirements

RadControls version

2008.03.1314.20
.NET version

2.0
Visual Studio version

VS 2005
programming language C#
browser support

all browsers supported by RadControls


PROJECT DESCRIPTION
This project illustrates how to export grouped data. The Export button in the group header will export only the current group. The trick behind this is to  loop through all the grouped header  items  and their children and hide the items  which do not have  the same Group index as the clicked one. As a result, the current group is exported.

CS:
using System; 
using System.Data; 
using System.Configuration; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 
using Telerik.Web.UI; 
 
public partial class _Default : System.Web.UI.Page  
    protected void Page_Load(object sender, EventArgs e) 
    { 
 
    } 
    protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e) 
    { 
         if (e.Item is GridGroupHeaderItem)  
         { 
           GridGroupHeaderItem item = e.Item as GridGroupHeaderItem; 
            LinkButton lnk = new LinkButton(); 
            lnk.ID = "lnkReservations"
             lnk.Text = "ExportGroup"
             lnk.Command += lnkReservations_Command;  
             item.DataCell.Controls.Add(lnk); 
     } 
      
    } 
    protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) 
    { 
         if (e.Item is GridGroupHeaderItem) 
         { 
         GridGroupHeaderItem item = e.Item as GridGroupHeaderItem; 
         LinkButton lnk = new LinkButton(); 
         lnk.ID = "lnkReservations"
         lnk.Text = "ExportGroup"
         item.DataCell.Controls.Add(lnk); 
       } 
    } 
  private void lnkReservations_Command(object sender, CommandEventArgs e) 
  { 
     LinkButton lnkReservations = (LinkButton)sender; 
     GridGroupHeaderItem currentHeaderitm = (GridGroupHeaderItem)lnkReservations.NamingContainer; 
     foreach (GridGroupHeaderItem headerItem in RadGrid1.MasterTableView.GetItems(GridItemType.GroupHeader)) 
     { 
         if (headerItem.GroupIndex != currentHeaderitm.GroupIndex) 
         { 
             GridItem[] children = headerItem.GetChildItems(); 
             foreach (GridItem child in children)  
             { 
                 GridDataItem dataItem = child as GridDataItem; 
                 dataItem.Visible = false
             } 
             headerItem.Visible = false
         } 
     } 
     RadGrid1.ExportSettings.OpenInNewWindow = true
     RadGrid1.ExportSettings.ExportOnlyData = true
     RadGrid1.MasterTableView.ExportToPdf(); 
  } 

ASPX:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 
 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"
<head runat="server"
    <title>Untitled Page</title> 
</head> 
<body> 
    <form id="form1" runat="server"
        <asp:ScriptManager ID="ScriptManager1" runat="server" /> 
        <div> 
            <telerik:RadGrid ID="RadGrid1" runat="server" Skin="Hay" DataSourceID="SqlDataSource1" GridLines="None" OnItemCreated="RadGrid1_ItemCreated" OnItemDataBound="RadGrid1_ItemDataBound"
                <HeaderContextMenu> 
                    <CollapseAnimation Duration="200" Type="OutQuint" /> 
                </HeaderContextMenu> 
                <MasterTableView AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
                     <GroupByExpressions> 
                       <telerik:GridGroupByExpression> 
                         <GroupByFields> 
                           <telerik:GridGroupByField FieldName="Country" FieldAlias="Country"  /> 
                         </GroupByFields> 
                         <SelectFields> 
                          <telerik:GridGroupByField  FieldName="Country" FieldAlias="Country" /> 
                         </SelectFields> 
                       </telerik:GridGroupByExpression> 
                     </GroupByExpressions> 
                    <Columns> 
                        <telerik:GridBoundColumn DataField="City" HeaderText="City" SortExpression="City" 
                            UniqueName="City"
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn DataField="EmployeeID" DataType="System.Int32" HeaderText="EmployeeID" 
                            SortExpression="EmployeeID" UniqueName="EmployeeID"
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn DataField="LastName" HeaderText="LastName" SortExpression="LastName" 
                            UniqueName="LastName"
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" 
                            UniqueName="FirstName"
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn DataField="Country" HeaderText="Country" SortExpression="Country" 
                            UniqueName="Country"
                        </telerik:GridBoundColumn> 
                    </Columns> 
                </MasterTableView> 
                <FilterMenu> 
                    <CollapseAnimation Duration="200" Type="OutQuint" /> 
                </FilterMenu> 
            </telerik:RadGrid> 
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthWindConnectionString %>" 
                SelectCommand="SELECT [City], [EmployeeID], [LastName], [FirstName], [Country] FROM [Employees]"
            </asp:SqlDataSource> 
        </div> 
    </form> 
</body> 
</html> 
 


1 Answer, 1 is accepted

Sort by
0
Accepted
Yavor
Telerik team
answered on 09 Mar 2009, 02:51 PM
Hi Princy,

This is a neat approach, and will undoubtedly be of good use to the community. Your Telerik points have been updated accordingly.

Best wishes,
Yavor
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.
Tags
Grid
Asked by
Princy
Top achievements
Rank 2
Answers by
Yavor
Telerik team
Share this question
or