I would like to print a grid which has a Hierarchy Child Template like the example like the example http://demos.telerik.com/silverlight/#GridView/Hierarchy/HierarchyChildTemplate.
When I try to print the grid only the parent rows are printed and none of the child rows are there.
Regards,
Joe
15 Answers, 1 is accepted
If you look at the code-behind for our Printing example in Silverlight, you'll notice that we create a print-friendly clone of the RadGridView that we feed to a PrintDocument. You only need to customize this RadGridView clone in code-behind to include a hierarchy child like the one in your actual RadGridView.
Kind regards,Yavor Georgiev
the Telerik team
Ive seen in the Print method where a print-friendly clone of a RadGridView is created, every column is copied from the original grid into the print grid.
What I'm not sure how or where you would add the Child Grid then add then add the Child grid columns into it.
Can you provide me an example of how to include a hierarchy child grid?
Regards,
Joe
You can do something like this:
RadGridView actualGrid;
RadGridView printGrid;
foreach (var item in actualGrid.ChildTableDefinitions)
{
printGrid.ChildTableDefinitions.add(item);
}
printGrid.HierarchyChildTemplate = actualGrid.HierarchyChildTemplate;
printGrid.HierarchyChildTemplate = (DataTemplate)this.Resources["printHierarchyChildTemplate"];
Please let me know if you need further assistance.
Greetings,
Yavor Georgiev
the Telerik team
The
printGrid.ChildTableDefinitions.add(item);
line cause the following compile error:'Telerik.Windows.Data.TableDefinitionCollection' does not contain a definition for 'add' and no extension method 'add' accepting a first argument of type 'Telerik.Windows.Data.TableDefinitionCollection' could be found (are you missing a using directive or an assembly reference?)
Regards,
Joe
Can you try Add() instead add()?
Regards,Vlad
the Telerik team
That fixed the compile error.
When I print it doesnt display the child grid. My print method is below.
private void PrintButton_Click(object sender, RoutedEventArgs e) |
{ |
offsetY = 0d; |
totalHeight = 0d; |
grid = new RadGridView(); |
grid.Width = 1550; |
grid.FontSize = 8; |
grid.DataContext = maintenanceGrid.DataContext; |
grid.ItemsSource = maintenanceGrid.ItemsSource; |
grid.RowIndicatorVisibility = Visibility.Collapsed; |
grid.ShowGroupPanel = false; |
grid.CanUserFreezeColumns = false; |
grid.IsFilteringAllowed = false; |
grid.AutoExpandGroups = true; |
grid.AutoGenerateColumns = false; |
foreach (GridViewColumn column in maintenanceGrid.Columns.OfType<GridViewColumn>()) |
{ |
GridViewColumn newColumn = (GridViewColumn)Activator.CreateInstance(column.GetType()); |
newColumn.CopyPropertiesFrom(column); |
grid.Columns.Add(newColumn); |
} |
foreach (var item in maintenanceGrid.ChildTableDefinitions) |
{ |
grid.ChildTableDefinitions.Add(item); |
} |
grid.HierarchyChildTemplate = maintenanceGrid.HierarchyChildTemplate; |
StyleManager.SetTheme(grid, StyleManager.GetTheme(maintenanceGrid)); |
grid.SortDescriptors.AddRange(maintenanceGrid.SortDescriptors); |
grid.GroupDescriptors.AddRange(maintenanceGrid.GroupDescriptors); |
grid.FilterDescriptors.AddRange(maintenanceGrid.FilterDescriptors); |
ScrollViewer.SetHorizontalScrollBarVisibility(grid, ScrollBarVisibility.Hidden); |
ScrollViewer.SetVerticalScrollBarVisibility(grid, ScrollBarVisibility.Hidden); |
PrintDocument doc = new PrintDocument(); |
canvas = new Canvas(); |
canvas.Children.Add(grid); |
doc.PrintPage += this.doc_PrintPage; |
doc.Print("RadGridView print"); |
} |
Do I have to add the GridViewColumns for the Child grid? And how and where would I do it?
Regards,
Joe
You need to expand your hierarchy items, because by default it is collapsed and won't show up when printing:
foreach
(var item
in
maintenanceGrid.ChildTableDefinitions)
{
grid.ChildTableDefinitions.Add(item);
}
grid.HierarchyChildTemplate = maintenanceGrid.HierarchyChildTemplate;
grid.ExpandAllHierarchyItems();
Yavor Georgiev
the Telerik team
How do I expand only the child rows that have child grids when printing? So I collapse all the child grids that don't have any data.
When I use grid.ExpandAllHierarchyItems() it expands all the child grids, even if there is no data for the child grid.
You can see this in the third row of attached jpg.
Another question is when printing a grid with child rows is there a way to suppress the Expandable icon for all the rows for the parent grid?
Finally is there a way to disable the selected row color(orange) in a datagrid. So that none of the rows are orange. When you have child grids all the first rows are highlighted orange and it appears when printed.
Regards,
Joe
To only expand rows that have a hierarchy child, replace the ExpandAllHierarchyItems() line with:
foreach
(MyDataClass item
in
grid.Items)
{
if
(item.MyCollectionProperty.Count > 0)
{
grid.ExpandHierarchyItem(item);
}
}
Unfortunately, there is no easy way to remove the expandable icon without editing the ControlTemplate of GridViewRow.
All the best,
Yavor Georgiev
the Telerik team
When I print the grid which contains a child grid. if I hide certain columns in both the parent grid and child grid so i can fit it to a A4 page size, the hiding of columns for the parent grid works fine but the hiding of columns for the child grid doesn't work no matter what I do. Is this because the child grid is in a data Template? How can I hide and show child grid columns when printing?
Regards,
Joe
Could you please open a support ticket and attach your project so that I can see how you perform the hiding?
Best wishes,Yavor Georgiev
the Telerik team
foreach
(MyDataClass item
in
grid.Items)
{
if
(item.MyCollectionProperty.Count > 0)
{
grid.ExpandHierarchyItem(item);
}
}
Kindly suggest me how to set child RadGridView font size while printing ?
Hi Yavor Georgiev,
I did follow the same code which you have mentioned below. But I am still not able to see the child elements in the Print or PrintPriview. I have replaced MyDataClass with my class name and the MyCollectionProperty to my Property collection. I have used the PrintAndExportExtensions class which is given by Telerik. When I debug the code it is able to go through the collection if any sub elements exists. Please let me know do I have to change any more properties?
foreach (MyDataClass item in grid.Items)
{
if (item.MyCollectionProperty.Count > 0)
{
grid.ExpandHierarchyItem(item);
}
}
As advised in the other forum thread - Print Preview Grid Hierarchy - the approach discussed in the current thread is not available anymore. Please, refer to the other thread for more information.
Regards,
Stefan Nenchev
Telerik