Articles in this section
Category / Section

How to customize print preview window in WPF DataGrid (SfDataGrid)?

2 mins read

WPF DataGrid (SfDataGrid) provides option to display print preview to review and customize the customize the PrintPreview window as per your requirement. In the below code example, we have customized the PrintPreview window with Excel and PDF exporting options.

XAML

<Grid>
        <Grid Background="#FFF7F7F7">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
…
            <Border Background="#FFDBDBDB" DataContext="{Binding ElementName=PrintPreviewArea}">
                <StackPanel Margin="10,5"
                            HorizontalAlignment="Center"
                            Orientation="Horizontal">...
                    
                    <Button Height="38"
                            Margin="10,3"
                            Click="OnExportPDFButtonClick"
                            Content="Export To PDF"
                            Style="{StaticResource ButtonStyle}"
                            ToolTip="Export To PDF" />
                    <Button Height="38"
                            Margin="10,3"
                            Click="OnExporttoExcelButtonClick"
                            Content="Export To Excel"
                            Style="{StaticResource ButtonStyle}"
                            ToolTip="Export To Excel" />
…
           </StackPanel>
       </Border>
 ... 
</Grid>                   

 

C#

void OnExporttoExcelButtonClick(object sender, RoutedEventArgs e)
{
    var dataGrid = Grid;
    if (dataGrid == null) return;
    try
    {
        //Setting the Exporting Options by craeting a instance for ExcelExportingOptions.
        var exportingOptions =new  ExcelExportingOptions();
        exportingOptions.ExportAllPages = true;
        exportingOptions.ExportStackedHeaders = true;
        //Below code exports datagrid to excel and returns Excel Engine.
        var excelEngine = dataGrid.ExportToExcel(dataGrid.View, exportingOptions);
 
        var workBook = excelEngine.Excel.Workbooks[0];
 
        //saving the workbook using savefiledialog.
        SaveFileDialog sfd = new SaveFileDialog
        {
            FilterIndex = 2,
            Filter = "Excel 97 to 2003 Files(*.xls)|*.xls|Excel 2007 to 2010 Files(*.xlsx)|*.xlsx"
        };
 
        if (sfd.ShowDialog() == true)
        {
            using (Stream stream = sfd.OpenFile())
            {
                if (sfd.FilterIndex == 1)
                    workBook.Version = ExcelVersion.Excel97to2003;
                else
                    workBook.Version = ExcelVersion.Excel2010;
                workBook.SaveAs(stream);
            }
 
            //Message box confirmation to view the created spreadsheet.
            if (MessageBox.Show("Do you want to view the workbook?", "Workbook has been created",
                                MessageBoxButton.YesNo, MessageBoxImage.Information) == MessageBoxResult.Yes)
            {
                //Launching the Excel file using the default Application.[MS Excel Or Free ExcelViewer]
                System.Diagnostics.Process.Start(sfd.FileName);
            }
        }
    }
    catch (Exception)
    {
 
    }
}
 
void OnExportPDFButtonClick(object sender, RoutedEventArgs e)
{
    var dataGrid = Grid;
    if (dataGrid == null) return;
    try
    {
        var options = new PdfExportingOptions();
        options.FitAllColumnsInOnePage = true;
        var document = dataGrid.ExportToPdf(options);
 
        SaveFileDialog sfd = new SaveFileDialog
        {
            Filter = "PDF Files(*.pdf)|*.pdf"
        };
 
        if (sfd.ShowDialog() == true)
        {
            using (Stream stream = sfd.OpenFile())
            {
                document.Save(stream);
            }
 
            //Message box confirmation to view the created Pdf file.
            if (MessageBox.Show("Do you want to view the Pdf file?", "Pdf file has been created",
                                MessageBoxButton.YesNo, MessageBoxImage.Information) == MessageBoxResult.Yes)
            {
                //Launching the Pdf file using the default Application.
                System.Diagnostics.Process.Start(sfd.FileName);
            }
        }
    }
    catch (Exception)
    {
 
    }
}

 

Customize the print preview window in WPF DataGrid

View sample in GitHub.

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied