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:
And here is what I'm doing to export:
Am I missing something obvious? Any help is much appreciated, thanks!
Jeremy
<
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