How to Print SfDataGrid with Header and Footer in UWP ?
You can print the SfDataGrid with Header and Footer content using PrintPageHeaderTemplate and PrintPageFooterTemplate properties in SfDataGrid.PrintSettings. By default, SfDataGrid prints without Header and Footer content.
The following XAML code example displays the Footer DataTemplate definition for SfDataGrid. While printing, this data template sets the ContentControl in PrintPageControl. From PrintPageControl, you can access the following properties using Ancestor binding in the data template.
- PageIndex – denotes the current page index that is printing.
- TotalPages – Total number of pages to be printed.
It is also possible to display and print the current DateTime using DateTime.Today as displayed in the following code example.
XAML
xmlns:system="clr-namespace:System;assembly=mscorlib" xmlns:syncfusion="http://schemas.syncfusion.com/wpf" <!--Need to add this code snippet in Resources Define the DataTemplate with Key--> <DataTemplate x:Key="FooterTemplate"> <Grid> <Image HorizontalAlignment="Left" Width="150" Height="35" Source="../Image/syncfusion.png"/> <TextBlock HorizontalAlignment="Center" FontSize="20" Text="{Binding Source={x:Static system:DateTime.Today}, StringFormat='{}{0:dddd, MMMM dd, yyyy}'}"/> <TextBlock Margin="0,0,10,0" HorizontalAlignment="Right" VerticalAlignment="Center" FontSize="20"> <TextBlock.Text> <Binding Path="PageIndex" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type syncfusion:PrintPageControl}}" StringFormat="Page : {0}" /> </TextBlock.Text> </TextBlock> </Grid> </DataTemplate>
The following code example displays how to set Footer DataTemplate (the above one) to PrintSettings.PrintPageFooterTemplate in SfDataGrid
C#
public MainWindow() { InitializeComponent(); this.SfdataGrid.PrintSettings.PrintPageFooterTemplate = Resources["FooterTemplate"] as DataTemplate; this.SfdataGrid.PrintSettings.PrintPageHeaderHeight = 35; this.SfdataGrid.PrintSettings.PrintPageFooterHeight = 35; this.SfdataGrid.PrintSettings.AllowRepeatHeaders = false; }
Using the same way, you can set PrintPageHeaderTemplate also while printing.
The following screenshot displays the print preview of SfDataGrid with Header and Footer.
Sample Links: