Skip to main content

Printing and Exporting

The following page provides a list of examples that demonstrate how to customize the appearance of printable views.

tip

All examples in this topic assume that the grid is bound to the Employees table of the Northwind database, unless stated otherwise.

Configuring a print view

The following example demonstrates how to use a PrintTableView and configure it to display a title in the page headers and the page number in the page footers. The elements added to these sections must be added as DataTemplates and will be repeated on each page.

The Print method will be called in the button's Click event, whose implementation is provided below.

  <Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"
xmlns:local="clr-namespace:Xceed.Wpf.Documentation">
<Grid.Resources>
<xcdg:DataGridCollectionViewSource x:Key="cvs_employees"
Source="{Binding Source={x:Static Application.Current},
Path=Employees}"/>
</Grid.Resources>

<DockPanel>
<Button Content="Print Grid"
Click="PrintGrid"
DockPanel.Dock="Top"/>
<xcdg:DataGridControl x:Name="EmployeesGrid"
ItemsSource="{Binding Source={StaticResource cvs_employees}}">
<xcdg:DataGridControl.PrintView>
<xcdg:PrintTableView>
<xcdg:PrintTableView.PageHeaders>
<DataTemplate>
<TextBlock Text="Xceed WPF Documentation"
HorizontalAlignment="Center"
FontWeight="Bold"
FontSize="20"/>
</DataTemplate>
<DataTemplate>
<TextBlock Text="Printing Example"
HorizontalAlignment="Center"
FontSize="16"/>
</DataTemplate>
</xcdg:PrintTableView.PageHeaders>
<xcdg:PrintTableView.PageFooters>
<DataTemplate>
<TextBlock Text="{xcdg:ViewBinding CurrentPageNumber}"
HorizontalAlignment="Right"/>
</DataTemplate>
</xcdg:PrintTableView.PageFooters>
</xcdg:PrintTableView>
</xcdg:DataGridControl.PrintView>
</xcdg:DataGridControl>
</DockPanel>
</Grid>

Configuring a progress window

The following example demonstrates how to change the default text displayed in the progress window when the Print or ExportToXps methods are called. The implementation of the PrintGrid method is provided below.

  <Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid">
<Grid.Resources>
<xcdg:DataGridCollectionViewSource x:Key="cvs_employees"
Source="{Binding Source={x:Static Application.Current},
Path=Employees}"/>
</Grid.Resources>
<DockPanel>
<Button Content="Print Employee Information"
Click="PrintGrid"
DockPanel.Dock="Top"/>
<xcdg:DataGridControl x:Name="EmployeesGrid"
ItemsSource="{Binding Source={StaticResource cvs_employees}}"
DockPanel.Dock="Bottom">
<xcdg:DataGridControl.PrintView>
<xcdg:PrintTableView>
<xcdg:PrintTableView.ProgressWindowDescription>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Printing page "/>
<TextBlock Text="{Binding CurrentPageNumber}"/>
<TextBlock Text=" of employee information..."/>
</StackPanel>
</xcdg:PrintTableView.ProgressWindowDescription>
</xcdg:PrintTableView>
</xcdg:DataGridControl.PrintView>
</xcdg:DataGridControl>
</DockPanel>
</Grid>

Styling a page

The following example demonstrates how to create a style to change the layout of the printed pages by providing a new ControlTemplate that will place the page headers and footers at the top of each page and display an orange border around the area where the grid is printed.

The Print method will be called in the button's Click event, whose implementation is provided below.

  <Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"
xmlns:local="clr-namespace:Xceed.Wpf.Documentation">
<Grid.Resources>
<xcdg:DataGridCollectionViewSource x:Key="cvs_employees"
Source="{Binding Source={x:Static Application.Current},
Path=Employees}"/>
<Style x:Key="page_style" TargetType="{x:Type xcdg:DataGridPageControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type xcdg:DataGridPageControl}">
<DockPanel>
<StackPanel xcdg:DataGridPageControl.IsPageHeadersHost="True"
DockPanel.Dock="Top"/>
<StackPanel xcdg:DataGridPageControl.IsPageFootersHost="True"
DockPanel.Dock="Top"/>
<Border BorderThickness="2"
BorderBrush="Orange"
xcdg:DataGridPageControl.IsDataGridHost="True"
DockPanel.Dock="Bottom"/>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<DockPanel>
<Button Content="Print Grid"
Click="PrintGrid"
DockPanel.Dock="Top"/>
<xcdg:DataGridControl x:Name="EmployeesGrid"
ItemsSource="{Binding Source={StaticResource cvs_employees}}">
<xcdg:DataGridControl.PrintView>
<xcdg:PrintTableView PageStyle="{StaticResource page_style}">
<xcdg:PrintTableView.PageHeaders>
<DataTemplate>
<TextBlock Text="Xceed WPF Documentation"
HorizontalAlignment="Center"
FontWeight="Bold"
FontSize="20"/>
</DataTemplate>
<DataTemplate>
<TextBlock Text="Printing Example"
HorizontalAlignment="Center"
FontSize="16"/>
</DataTemplate>
</xcdg:PrintTableView.PageHeaders>
<xcdg:PrintTableView.PageFooters>
<DataTemplate>
<StackPanel HorizontalAlignment="Right"
Orientation="Horizontal">
<TextBlock Text="Page "/>
<TextBlock Text="{xcdg:ViewBinding CurrentPageNumber}"/>
</StackPanel>
</DataTemplate>
</xcdg:PrintTableView.PageFooters>
</xcdg:PrintTableView>
</xcdg:DataGridControl.PrintView>
</xcdg:DataGridControl>
</DockPanel>
</Grid>

Exporting to Excel (ExcelExporter Class)

The following example demonstrates how to export the content of a grid to Excel using the ExcelExporter class.

    <Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid">
<Grid.Resources>
<xcdg:DataGridCollectionViewSource x:Key="cvs_orders"
Source="{Binding Source={x:Static Application.Current}, Path=Orders}">
<xcdg:DataGridCollectionViewSource.GroupDescriptions>
<xcdg:DataGridGroupDescription PropertyName="ShipCountry" />
</xcdg:DataGridCollectionViewSource.GroupDescriptions>

<xcdg:DataGridCollectionViewSource.StatFunctions>
<xcdg:AverageFunction SourcePropertyName="Freight"
ResultPropertyName="average_freight" />
</xcdg:DataGridCollectionViewSource.StatFunctions>
</xcdg:DataGridCollectionViewSource>
</Grid.Resources>
<DockPanel>
<Button Content="Export"
Click="ExportButton_Click"
DockPanel.Dock="Top" />
<xcdg:DataGridControl x:Name="OrdersGrid"
ItemsSource="{Binding Source={StaticResource cvs_orders}}"
AutoCreateDetailConfigurations="True">
<xcdg:DataGridControl.DefaultGroupConfiguration>
<xcdg:GroupConfiguration>
<xcdg:GroupConfiguration.Footers>
<DataTemplate>
<xcdg:StatRow>
<xcdg:StatCell FieldName="Freight"
ResultPropertyName="average_freight"/>
</xcdg:StatRow>
</DataTemplate>
</xcdg:GroupConfiguration.Footers>
</xcdg:GroupConfiguration>
</xcdg:DataGridControl.DefaultGroupConfiguration>
</xcdg:DataGridControl>
</DockPanel>
</Grid>

Exporting to Excel (ExportToExcel Method)

The following example demonstrates how to export the content of a grid to Excel using the grid's ExportToExcel method.

  <Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid">
<Grid.Resources>
<xcdg:DataGridCollectionViewSource x:Key="cvs_orders"
Source="{Binding Source={x:Static Application.Current}, Path=Orders}">
<xcdg:DataGridCollectionViewSource.GroupDescriptions>
<xcdg:DataGridGroupDescription PropertyName="ShipCountry" />
</xcdg:DataGridCollectionViewSource.GroupDescriptions>
</xcdg:DataGridCollectionViewSource>
</Grid.Resources>
<DockPanel>
<Button Content="Export"
Click="ExportButton_Click"
DockPanel.Dock="Top" />
<xcdg:DataGridControl x:Name="OrdersGrid"
ItemsSource="{Binding Source={StaticResource cvs_orders}}"/>
</DockPanel>
</Grid>

Changing the group-header formats

The following example demonstrates how to change the text that is displayed in the group headers when they are exported to Excel.

  <Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid">
<Grid.Resources>
<xcdg:DataGridCollectionViewSource x:Key="cvs_orders"
Source="{Binding Source={x:Static Application.Current}, Path=Orders}">
<xcdg:DataGridCollectionViewSource.GroupDescriptions>
<xcdg:DataGridGroupDescription PropertyName="ShipCountry" />
</xcdg:DataGridCollectionViewSource.GroupDescriptions>
</xcdg:DataGridCollectionViewSource>
</Grid.Resources>
<DockPanel>
<Button Content="Export"
Click="ExportButton_Click"
DockPanel.Dock="Top" />
<xcdg:DataGridControl x:Name="OrdersGrid"
ItemsSource="{Binding Source={StaticResource cvs_orders}}"/>
</DockPanel>
</Grid>