Category / Section
How to print multiple WPF DataGrid (SfDataGrid) in a single document?
2 mins read
WPF DataGrid (SfDataGrid) does not have direct support to print multiple SfDataGrid in single document. But you can achieve this by using Syncfuion ReportViewer control which will print multiple WPF DataGrid (SfDataGrid) in a single document. You have to create a RDLC file with DataSet and also you need to set DataSources for ReportViewer from SfDataGrid.ItemsSource property like below code example.
For more details about creating RDLC file, please click here.
XAML
<syncfusion:SfDataGrid Name="grid2" AutoGenerateColumns="True" EditTrigger="OnDoubleTap" ItemsSource="{Binding EmployeeDetails}" /> <StackPanel Grid.Column="1"> <Button x:Name="GenerateReport" Click="GenerateReport_Click" Content="Generate Report" /> </StackPanel> <syncfusion:SfDataGrid Name="grid1" Grid.Column="2" AutoGenerateColumns="True" EditTrigger="OnDoubleTap" ItemsSource="{Binding EmployeeDetails1}" />
C#
private void GenerateReport_Click(object sender, RoutedEventArgs e) { ReportViewer reportViewer1 = new ReportViewer(); reportViewer1.ProcessingMode = ProcessingMode.Local; reportViewer1.ReportPath = @"../../Report1.rdlc"; reportViewer1.DataSources.Clear(); //Add Datasource and set ItemsSource for grid1. reportViewer1.DataSources.Add(new Syncfusion.Windows.Reports.ReportDataSource() { Name = "DataSet1", Value = grid1.ItemsSource }); //Add Datasource and set ItemsSource for grid1. reportViewer1.DataSources.Add(new Syncfusion.Windows.Reports.ReportDataSource() { Name = "DataSet2", Value = grid2.ItemsSource }); Print rv = new Print(); rv.Content = reportViewer1; reportViewer1.RefreshReport(); rv.Show(); }
In UWP, you have to use ReportViewer in a separate page and you have to navigate to the corresponding page through button click. Please refer the below code example to create the separate page with ReportViewer.
XAML
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Reports:SfReportViewer x:Name="ReportViewer" > </Reports:SfReportViewer> </Grid>
C#
private void GenerateReport_Click(object sender, RoutedEventArgs e) { object[] grids = { grid1, grid2,DataContext }; this.Frame.Navigate(typeof(BlankPage1), grids); } protected override async void OnNavigatedTo(NavigationEventArgs e) { await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, new Windows.UI.Core.DispatchedHandler(() => { var grids = e.Parameter; var gridArray = grids as object[]; var grid1 = gridArray[0] as SfDataGrid; var grid2 = gridArray[1] as SfDataGrid; Assembly assembly = typeof(BlankPage1).GetTypeInfo().Assembly; Stream reportStream = assembly.GetManifestResourceStream("SfDataGridDemo.Report1.rdlc"); this.ReportViewer.ProcessingMode = ProcessingMode.Local; this.ReportViewer.ExportMode = ExportMode.Local; this.ReportViewer.LoadReport(reportStream); this.ReportViewer.DataSources.Clear(); //Add Datasource and set ItemsSource for grid1. this.ReportViewer.DataSources.Add(new ReportDataSource { Name = "DataSet1", Value = grid1.ItemsSource as ObservableCollection<Model> }); //Add Datasource and set ItemsSource for grid1. this.ReportViewer.DataSources.Add(new ReportDataSource { Name = "DataSet2", Value = grid2.ItemsSource as ObservableCollection<Model> }); this.ReportViewer.RefreshReport(); })); }