)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to customize print preview window in SfDataGrid?

Platform: WPF |
Control: SfDataGrid |
Published Date: May 13, 2016 |
Last Revised Date: February 10, 2020
Tags: printing, styling

In SfDataGrid, you can customize the PrintPreview window as per your requirement. In the below code example, we have customize 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)
    {
 
    }
}

 

Sample link:

WPF

Screenshot:

C:\Users\gnanasownthari.thiru\Desktop\Capture.PNG

 

 

 

 

2X faster development

The ultimate WPF UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon