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

Exporting hierarchy of grids to excel

6 Answers 207 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jeremy
Top achievements
Rank 1
Jeremy asked on 22 Jul 2011, 11:51 PM
I'm trying to export a hierarchy of 2 grids, where a property of one item in the grid is a collection of other items displayed in a child grid using HierarchyChildTemplate. I've been following several examples I've found online using the ElementExported event and DataTemplates for the export formatting for each grid, and I can currently get the parent grid to export to excel, but not the child grid. The child grid's column headers print however, but no data is printed. I've found that the chid grid's ElementExported event is not even firing either. Here is how I have the grids set up:

<Telerik:RadGridView x:Name="ChecksGrid" CanUserResizeColumns="True" Telerik:StyleManager.Theme="Summer" CanUserInsertRows="False" IsFilteringAllowed="False" ElementExported="ChecksGrid_ElementExported"
                               AutoGenerateColumns="False" HorizontalAlignment="Stretch" Margin="0" VerticalAlignment="Top" Cursor="Hand" CanUserReorderColumns="False" ShowGroupPanel="False"
                               CanUserSortColumns="False" SelectionMode="Single" BorderThickness="0" VirtualizingStackPanel.VirtualizationMode="Recycling" ShowColumnFooters="True">
                    <Telerik:RadGridView.ChildTableDefinitions>
                        <Telerik:GridViewTableDefinition />
                    </Telerik:RadGridView.ChildTableDefinitions>
 
                    <Telerik:RadGridView.HierarchyChildTemplate>
                        <DataTemplate>
                            <Telerik:RadGridView x:Name="RefundsAndMiscGrid" CanUserFreezeColumns="False" AutoGenerateColumns="False" ItemsSource="{Binding RefundsAndMiscCollection}" Loaded="RefundsAndMiscGrid_Loaded" ElementExported="RefundsAndMiscGrid_ElementExported"
                                                 ShowColumnFooters="True" ShowGroupFooters="True" Telerik:StyleManager.Theme="Vista"  ShowGroupPanel="False" IsReadOnly="True" CanUserSortColumns="False" CanUserResizeColumns="True">

And here is what I'm doing to export:

private void ChecksGrid_ElementExported(object sender, GridViewElementExportedEventArgs e)
        {
            if (e.Element == ExportElement.Row)
            {
                var template = this.LayoutRoot.Resources["ChecksHierarchy"] as DataTemplate;
                var grid = template.LoadContent() as RadGridView;
                grid.DataContext = e.Context;
                var subExport = grid.ToHtml();
                var columns = this.ChecksGrid.Columns.OfType<GridViewBoundColumnBase>().Count();
                var row = string.Format(@"<tr><td style=""background-color:#CCC;"" colspan=""{0}"">{1}</td></tr>", columns, subExport);
                e.Writer.Write(row);
            }
        }
 
        private void RefundsAndMiscGrid_ElementExported(object sender, GridViewElementExportedEventArgs e)
        {
            if (e.Element == ExportElement.Row)
            {
                var template = this.LayoutRoot.Resources["RefundsAndMiscHierarchy"] as DataTemplate;
                var grid = template.LoadContent() as RadGridView;
                grid.DataContext = e.Context;
                var subExport = grid.ToHtml();
                var columns = this.ChecksGrid.Columns.OfType<GridViewBoundColumnBase>().Count();
                var row = string.Format(@"<tr><td style=""background-color:#C00;"" colspan=""{0}"">{1}</td></tr>", columns, subExport);
                e.Writer.Write(row);
            }
        }
 
        private void ExportBtn_Click(object sender, RoutedEventArgs e)
        {
            string extension = "xls";
            string selectedItem = "Excel";
            ExportFormat format = ExportFormat.Html;
 
            SaveFileDialog dialog = new SaveFileDialog();
            dialog.DefaultExt = extension;
            dialog.Filter = String.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*", extension, selectedItem);
            dialog.FilterIndex = 1;
 
            if (dialog.ShowDialog() == true)
            {
                using (Stream stream = dialog.OpenFile())
                {
                    GridViewExportOptions options = new GridViewExportOptions();
                    options.Format = format;
                    options.ShowColumnHeaders = true;
                    options.Encoding = Encoding.UTF8;
                    ChecksGrid.Export(stream, options);
                }
            }
        }


Am I missing something obvious? Any help is much appreciated, thanks!

Jeremy

6 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 25 Jul 2011, 01:17 PM
Hi Jeremy,

 As I can see from you code, you have missed to write the subExport to e.Writer, for example:

private void clubsGrid_ElementExported(object sender, GridViewElementExportedEventArgs e)
        {
                var template = this.LayoutRoot.Resources["RowDetailsTemplate"] as DataTemplate;
                var grid = template.LoadContent() as RadGridView;
                grid.DataContext = e.Context;
                var subExport = grid.ToHtml();
                e.Writer.Write(subExport);
        }

I am attaching a sample project.

Does this solve your problem?

Greetings,
Didie
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

0
Jeremy
Top achievements
Rank 1
answered on 25 Jul 2011, 11:07 PM
I'm doing an e.Writer.Write(row); for each of the element exported events.. am I missing something else?

Thanks,

Jeremy

Edit: Ok scratch that, I didn't quite understand how the exporting works, but I think I have it now :)
0
Swarna
Top achievements
Rank 1
answered on 23 May 2012, 12:25 PM
Hello,
When we run the sample application; the exported excel gives distorted parent rows from 2nd record onwards.
PFA the output screenshot and please provide the solution
Thanks
Swarna
0
Anne Lyon
Top achievements
Rank 1
answered on 23 Jan 2015, 08:47 AM
Hello,
Is there an answer to this? When I run your hierarchical export example (287458_RadGridView-ExportingHierarchyTemplate) I get the same result as in the jpg in the previous post. From row 2 and onwards in the parent grid all cell values are just concatinated into one string and inserted into first cell. Although your sample-project is old, running the same code with Version 2014.2 does not make any difference.
Is there a fix to this? Apart from this snag with the parent rows the export is working fine.
0
Anne Lyon
Top achievements
Rank 1
answered on 23 Jan 2015, 01:55 PM
Just wanted to tell everyone that I found a wokraround for the problem in the RadGridView_ExportingHierarchyTemplate example where, by using the option2 method (e-Writer.Write(subExport)), 2nd row and onwards of the parent records turned ontp one concatenated string in forst row-cell.
In clubsGrid_ElementExported put this code in instead of the 2 options:

// best option!
Club obj = e.Context as Club;
if (obj != null)
{

e.Writer.Write(String.Format(@"<tr><td style=""background-color:#EEE;"" colspan=""{0}"">", 5));
e.Writer.Write(String.Format(@"<table border=""1"" style=""border-collapse:collapse"">"));
e.Writer.Write(String.Format(@"<tr><td></td><td><b>Name</b></td><td>Number</td><td>Position</td><td>Country</td></tr>"));

foreach (Player player in obj.Players)
{
e.Writer.Write(String.Format(@"<tr><td></td><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td></tr>", player.Name, player.Number, player.Country, player.Position));
}
e.Writer.Write(String.Format(@"<tr></tr></table>"));

}

For formatting of parent rows put this in clubsGrid_ElementExporting:
var visParameters = e.VisualParameters as GridViewHtmlVisualExportParameters; 
            if (e.Element == ExportElement.HeaderRow)
{
visParameters.FontSize = 16;
visParameters.FontWeight = FontWeights.Bold;
visParameters.Background = System.Windows.Media.Colors.LightGray;
}
else if (e.Element == ExportElement.Row)
{
visParameters.FontSize = 12;
}

0
Boris
Telerik team
answered on 26 Jan 2015, 02:12 PM
Hello Anne,

I am glad that you have found a resolution to the issue and shared it with our community.

Regards,
Boris
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
GridView
Asked by
Jeremy
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Jeremy
Top achievements
Rank 1
Swarna
Top achievements
Rank 1
Anne Lyon
Top achievements
Rank 1
Boris
Telerik team
Share this question
or