I am making an accounting program using ( uwp , c#, MySQL ), I want to know when printing the reports if the datagrid is long how to extend the rest to new pages so that all report will be printed on multiple pages.
i used printhelp.cs that is in the printing sample: https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/Printing , but it printed the datagrid only on one page and didn't extend the rest to more pages.
UWP XAML:
<Grid>
<controls:DataGrid Grid.Row="2" Grid.RowSpan="5" Grid.ColumnSpan="2"
x:Name="TextContent"
Foreground="Black"
Background="White"
ItemsSource="{x:Bind SelectSOA()}"
AutoGenerateColumns="False"
GridLinesVisibility="Horizontal">
<controls:DataGrid.Columns>
<controls:DataGridTextColumn Header="ID" Binding="{Binding ID}" />
<controls:DataGridTextColumn Header="Account" Binding="{Binding Principal}" />
<controls:DataGridTextColumn Header="Balance" Binding="{Binding Client}" />
</controls:DataGrid.Columns>
<RichTextBlockOverflow x:Name="FirstLinkedContainer" OverflowContentTarget="{Binding ElementName=ContinuationPageLinkedContainer}" Grid.Row="2" Grid.Column="0"/>
<RichTextBlockOverflow x:Name="ContinuationPageLinkedContainer" Grid.Row="3" Grid.ColumnSpan="2"/>
</Grid>
i expect when printing if the Datagrid is long to extend to next page. but it only prints the first page and does not extend the rest to new pages.
You could use the Print Helper class to print the DataGrid, but you still need to manually paging your data in code-behind.
Please see the Windows Community Toolkit PrintHelper sample to learn how to use this class to print the XAML control.
According to that sample, I do a little changes to make a simple code sample for your reference:
<Grid>
<Grid x:Name="RootGrid"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid x:Name="Container"
Grid.RowSpan="2"
Opacity="0" />
<Grid x:Name="CustomPrintContainer"
Opacity="0" />
<Grid x:Name="DirectPrintContainer">
<Grid x:Name="PrintableContent">
<Grid x:Name="XamlRoot" />
</Grid>
</Grid>
</Grid>
<Button Content="Print" Click="Button_Click" VerticalAlignment="Bottom"></Button>
</Grid>
public List<Person> Persons { get; set; }
public MainPage()
{
this.InitializeComponent();
Persons = new List<Person>();
for (int i = 0; i < 100; i++)
{
Persons.Add(new Person
{
PersonId = i,
FirstName = "FirstName" + i,
LastName = "LastName" + i,
Position = "Network Administrator " + i
});
}
}
private PrintHelper _printHelper;
private async void Button_Click(object sender, RoutedEventArgs e)
{
_printHelper = new PrintHelper(CustomPrintContainer);
var pageNumber = 0;
for (int i = 0; i < Persons.Count; i = i + 10)
{
var grid = new Grid();
grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
// Static header
var header = new TextBlock { Text = "Custom Print", Margin = new Thickness(0, 0, 0, 20) };
Grid.SetRow(header, 0);
grid.Children.Add(header);
// Main content with layout from data template
var dataGrid = new DataGrid();
dataGrid.AutoGenerateColumns = true;
dataGrid.ItemsSource = Persons.Skip(i).Take(10);
Grid.SetRow(dataGrid, 1);
grid.Children.Add(dataGrid);
// Footer with page number
pageNumber++;
var footer = new TextBlock { Text = string.Format("page {0}", pageNumber), Margin = new Thickness(0, 20, 0, 0) };
Grid.SetRow(footer, 2);
grid.Children.Add(footer);
_printHelper.AddFrameworkElementToPrint(grid);
}
_printHelper.OnPrintCanceled += _printHelper_OnPrintCanceled;
_printHelper.OnPrintFailed += _printHelper_OnPrintFailed;
_printHelper.OnPrintSucceeded += _printHelper_OnPrintSucceeded;
var printHelperOptions = new PrintHelperOptions(false);
printHelperOptions.Orientation = Windows.Graphics.Printing.PrintOrientation.Default;
printHelperOptions.AddDisplayOption(StandardPrintTaskOptions.Orientation);
await _printHelper.ShowPrintUIAsync("print sample", printHelperOptions);
}
public class Person
{
public int PersonId { get; set; }
public int DepartmentId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Position { get; set; }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With