How to Create a Print Preview Dialog in a WinForms Datagrid
Quick Start Guide | |
---|---|
What You Will Need |
Visual Studio |
Controls Referenced | |
Tutorial Concept | Discover how to create a modernized datagrid print preview dialog in C# .NET Windows Forms using ComponentOne FlexGrid, Print Preview, and FlexViewer controls. |
ComponentOne's FlexGrid for WinForms is a powerful control for displaying data in .NET C# applications. To provide professional, feature-rich print preview capabilities, users are upgrading from older controls, such as C1PrintPreviewControl, to the advanced FlexViewer. This post outlines the range of print preview options for FlexGrid and provides an example of the best-practice method for utilizing FlexViewer with FlexGrid.
In this blog, we'll cover:
- Built-in Print Preview Dialog
- Print Preview Using C1PrintPreview and C1RibbonPreview
- Exporting to PDF
- Modernizing FlexGrid Print Preview with FlexViewer
Ready to get started? Download ComponentOne Today!
Built-in Print Preview Dialog
The fastest method uses the FlexGrid's own PrintGrid method. It automatically generates and displays a basic print preview dialog, giving control over the dialog window's properties before printing.
Form printPreview = c1FlexGrid1.PrintParameters.PrintPreviewDialog as Form;
printPreview.Text = "Order Details";
c1FlexGrid1.PrintGrid("Orders", PrintGridFlags.ShowPreviewDialog);
As you can see, the built-in print preview is very basic, but it gets the job done with very minimal code.
Use the FlexGrid's PrintParameters property to specify printing options such as header and footer fonts, page margins, and page orientation. Among the printing options is the PrintGridFlags property, which allows you to choose multiple options for rendering the print. These include:
- ActualSize - Prints the grid in actual screen size. Extra rows and columns will spill onto additional pages.
- ExtendedLastCol - Extends the last column so that the FlexGrid always takes up at least the full page width.
- FitToPage - Scales the FlexGrid both horizontally and vertically to fit all content on one page.
- FitToPageWidth - Scales the FlexGrid only horizontally, ensuring all columns fit on a single page. Rows will spill onto multiple pages if necessary (with repeated column headers).
Print Preview Using C1PrintPreview and C1RibbonPreview
ComponentOne also provides a collection of advanced print preview controls, which are part of PrintPreview for WinForms. Use the C1PrintPreviewDialog or the C1RibbonPreviewDialog in conjunction with C1FlexGrid to enhance the printing functionality and make the user experience much more impressive. Here's how:
- Add a C1PrintPreviewDialog or C1RibbonPreviewDialog control to your form.
- Specify Print Document Options. For example, below, we specify the option of FitToPageWidth:
// get print parameters
var pp = this.c1FlexGrid1.PrintParameters;
// specify print document options
pp.PrintGridFlags = C1.Win.C1FlexGrid.PrintGridFlags.FitToPageWidth;
The final step is to assign the FlexGrid's PrintDocument, which can also be accessed through the print parameters, to the print preview control.
// show the print document in a C1PrintPreview control
c1PrintPreviewDialog1.Document = pp.PrintDocument;
c1PrintPreviewDialog1.ShowDialog();
The ComponentOne Print Preview controls can render any PrintDocument; just set one to its Document property. Then, launch the dialog with the ShowDialog method. Do this all on a button click or some other event in your application:
The following NuGet packages are required to use the C1PrintPreviewDialog with FlexGrid.
- C1.Win.FlexGrid
- C1.Win.Printing
Print Preview with a Ribbon UI
The C1RibbonPreviewDialog is the exact same control as C1PrintPreviewDialog, but features a more modern Ribbon UI instead of the toolstrip. In applications built with the ComponentOne Ribbon control, the C1RibbonPreviewDialog provides a dedicated, feature-rich print dialog. The same code binds the grid's print data directly to the dialog's document property.
C1RibbonPreviewDialog ribbonDialog = new C1RibbonPreviewDialog();
ribbonDialog.Document = c1FlexGrid1.PrintParameters.PrintDocument;
ribbonDialog.Text = "Order Details";
ribbonDialog.StartPosition = FormStartPosition.CenterParent;
ribbonDialog.WindowState = FormWindowState.Normal;
ribbonDialog.ShowDialog();
The following NuGet packages are required to use the C1RibbonPreviewDialog with C1FlexGrid.
- C1.Win.FlexGrid
- C1.Win.Printing
- C1.Win.Ribbon
Direct Export to PDF
The FlexGrid document model can print directly to a PDF file using the "Microsoft Print to PDF" virtual printer. In the next step, we will learn how to use PDF files to display our FlexGrid in FlexViewer.
var gridDocument = c1FlexGrid1.PrintParameters.PrintDocument;
gridDocument.DocumentName = "Export to PDF";
gridDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
gridDocument.Print();
Modernizing FlexGrid Print Preview with FlexViewer
The most robust and feature-complete solution is to utilize the FlexViewer. This approach unlocks features like document navigation via thumbnails, integrated search, and superior exporting.
This technique involves two steps: exporting the grid data to a temporary PDF file and then loading that file into the C1FlexViewer. This is the most effective way to modernize the print preview experience.
- The C1FlexGrid's PrintDocument is configured to print to a file (tempPdfPath).
- The resulting PDF file is loaded using C1PdfDocumentSource.
- The C1PdfDocumentSource is assigned to the DocumentSource property of the FlexViewer, utilizing the viewer's advanced capabilities.
// Create temporary PDF file
string tempPdfPath = Path.Combine(Path.GetTempPath(), $"GridExport_{DateTime.Now:yyyyMMdd_HHmmss}.pdf");
// Export grid to PDF file
var printDocument = c1FlexGrid1.PrintParameters.PrintDocument;
printDocument.DocumentName = "Order Details";
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
printDocument.PrinterSettings.PrintToFile = true;
printDocument.PrinterSettings.PrintFileName = tempPdfPath;
printDocument.Print();
// Load PDF into FlexViewer using C1PdfDocumentSource
C1FlexViewer flexViewer = new C1FlexViewer();
C1PdfDocumentSource pdf = new C1PdfDocumentSource();
pdf.DocumentLocation = tempPdfPath;
flexViewer.DocumentSource = pdf;
// Create form to host FlexViewer
Form viewerForm = new Form();
viewerForm.Text = "Order Details - FlexViewer";
viewerForm.StartPosition = FormStartPosition.CenterParent;
viewerForm.Size = new Size(1000, 700);
flexViewer.Dock = DockStyle.Fill;
viewerForm.Controls.Add(flexViewer);
viewerForm.ShowDialog();
The following NuGet packages are required to work with FlexViewer and FlexGrid.
- C1.Win.FlexGrid
- C1.Win.FlexViewer
Ready to try it out? Download ComponentOne Today!
Conclusion
Developers have several options for managing print preview for FlexGrid data. While the built-in methods offer simplicity, transitioning to the FlexViewer workflow using PDF provides the most modern, powerful, and feature-rich document viewing experience in C# .NET applications. If you're looking for a quick and modern solution, the C1RibbonPreview may be the best choice for you.