Print and Export Data
- 8 minutes to read
#Print Data in Code
Reference the DevExpress.WinUI.Grid.Printing
namespace and call the Print method to print the GridControl on the default system printer. You can also pass a printer’s name as a parameter to print the GridControl on this printer.
using DevExpress.WinUI.Grid.Printing;
// ...
// Print the GridControl on the default printer
gridControl.Print();
// Print the GridControl on the specified printer
gridControl.Print("HP LaserJet 1022");
#Export Data in Code
Follow the steps below to export GridControl data in code-behind:
Reference the
DevExpress.WinUI.Grid.Printing
andDevExpress.WinUI.Printing
namespaces.Call the Export method.
Specify the export format and the full path to the created file.
using DevExpress.WinUI.Grid.Printing;
using DevExpress.XtraPrinting;
// ...
gridControl.Export(ExportFormats.Csv, @"C:\Users\user\Documents\GridData.csv");
#Print/Export Data with Print Preview
The Print Preview allows users to print a document and export it to a file in the required format. In DevExpress WinUI Controls, the DocumentViewer control is used to display a Print Preview.
Follow the steps below to print/export the GridControl data in Print Preview:
- Reference the
DevExpress.WinUI.Grid.Printing
andDevExpress.WinUI.Printing
namespaces. Create a GridControlPrinter instance that contains the GridControl you want to print/export. Pass the created GridControlPrinter instance to the PrintableLink‘s constructor as a parameter to create a PrintableLink instance. The PrintableLink class creates a link between a GridControl and a printed document.
using DevExpress.WinUI.Grid.Printing; using DevExpress.WinUI.Printing; //... var printer = new GridControlPrinter(gridControl); PrintableLink printDocument = new PrintableLink(printer);
Assign the created PrintableLink instance to a DocumentViewer‘s DocumentSource property to display a GridControl‘s Print Preview. The DocumentViewer includes extended API that you can use to export, print (with the print dialog), and save the GridControl‘s data.
using DevExpress.WinUI.Grid.Printing; using DevExpress.WinUI.Printing; //... var printer = new GridControlPrinter(gridControl); PrintableLink printDocument = new PrintableLink(printer) documentViewer.DocumentSource = printDocument;
Choose one of the following:
Use the ShowPrintDialogCommand or click the
Print...
button in the DocumentViewer’s toolbar to display the print dialog where you can print the opened document.Reference the
DevExpress.WinUI.DocumentViewer
namespace and call the DocumentViewer’s Export method to export the opened document as a file in different formats. You can also use the ShowExportDialogCommand property or click theExport...
button in the DocumentViewer’s toolbar to display the Export dialog.using DevExpress.WinUI.Grid.Printing; using DevExpress.WinUI.Printing; using DevExpress.WinUI.DocumentViewer; //... var printer = new GridControlPrinter(gridControl); PrintableLink printDocument = new PrintableLink(printer) documentViewer.DocumentSource = printDocument; documentViewer.Export(ExportFormats.Csv, @"C:\Users\user\Documents\GridData.csv");
#Customize Appearance
#Customize GridControl Print Appearance
You can use the following properties to specify the GridControl elements to print and customize their appearance settings:
Property | Description |
---|---|
ColumnBase.AllowPrint | Gets or sets whether to display the column in the printed/exported document. This is a dependency property. |
ColumnBase.PrintCellStyle | Gets or sets the style applied to cells in a printed document. This is a dependency property. |
GridControl.PrintAllGroupsExpanded | Gets or sets whether to print the grid with all group rows expanded. This is a dependency property. |
GridControl.PrintAutoWidth | Gets or sets whether columns should fit to the report page in a printed document. This is a dependency property. |
GridControl.PrintColumnHeaders | Gets or sets whether to print column headers. This is a dependency property. |
GridControl.PrintColumnHeaderStyle | Gets or sets the style applied to column header items in a printed document. This is a dependency property. |
GridControl.PrintFixedTotalSummary | Gets or sets whether to print the fixed total summary panel. This is a dependency property. |
GridControl.PrintFixedTotalSummaryStyle | Gets or sets the style applied to fixed total summary items in a printed document. This is a dependency property. |
GridControl.PrintGroupLevelIndent | Gets or sets the indent between group rows at neighboring group levels when the GridControl is printed. This is a dependency property. |
GridControl.PrintGroupRowStyle | Gets or sets the style applied to group row items (including Group Summary) in a printed document. This is a dependency property. |
GridControl.PrintGroupSummary | Gets or sets whether to print the group summary panel. This is a dependency property. |
GridControl.PrintTotalSummary | Gets or sets whether to print the summary panel. This is a dependency property. |
GridControl.PrintTotalSummaryStyle | Gets or sets the style applied to total summary items when they are printed. This is a dependency property. |
The following code sample specifies the Quantity column’s background color and font weight and hides the total summary panel from the print document:
<Page ...
xmlns:dxg="using:DevExpress.WinUI.Grid">
<Grid RowDefinitions="Auto,*">
<!-- ... -->
<dxg:GridControl
x:Name="gridControl" Grid.Row="1"
ItemsSource="{x:Bind ViewModel.Products}" AutoGenerateColumns="False"
ShowTotalSummary="True" PrintTotalSummary="False">
<dxg:GridControl.Columns>
<dxg:GridTextColumn FieldName="ProductName" />
<dxg:GridTextColumn FieldName="Country" />
<dxg:GridTextColumn FieldName="City" />
<dxg:GridTextColumn FieldName="Quantity">
<dxg:GridTextColumn.PrintCellStyle>
<dxg:GridPrintBrickStyle FontWeight="Bold" Background="LightSeaGreen"/>
</dxg:GridTextColumn.PrintCellStyle>
</dxg:GridTextColumn>
<dxg:GridTextColumn FieldName="UnitPrice" />
</dxg:GridControl.Columns>
<dxg:GridControl.TotalSummary>
<dxg:GridTotalSummaryItem FieldName="Quantity" SummaryType="Sum" />
</dxg:GridControl.TotalSummary>
</dxg:GridControl>
</Grid>
</Page>
using DevExpress.WinUI.Grid.Printing;
using DevExpress.WinUI.Printing;
private void PrintButton_Click(object sender, RoutedEventArgs e) {
gridControl.Print();
}
#Customize Document Settings
You can use the following properties to define a document’s print settings (margins, footer and header text/appearance, paper kind, and more):
Property | Description |
---|---|
LinkBase.Margins | Gets or sets the margins of a report page (in hundredths of an inch). |
LinkBase.MinMargins | Gets or sets the minimum printer margins. |
LinkBase.PaperKind | Gets or sets the paper type. |
Link.BottomMarginStyle | Gets or sets the style applied to a bottom margin of the printed document. |
Link.BottomMarginText | Gets or sets the text of the document’s bottom margin. |
Link.ColumnLayout | Gets or sets the column layout. |
Link.ColumnWidth | Gets or sets the width of a single column. |
Link.Landscape | Gets or sets whether the printed page orientation is landscape. |
Link.PageFooterStyle | Gets or sets the style applied to a footer of the printed page. |
Link.PageFooterText | Gets or sets the text of the page’s footer. |
Link.PageHeaderStyle | Gets or sets the style applied to a header of the printed page. |
Link.PageHeaderText | Gets or sets the text of the page’s header. |
Link.PrintReportFooterAtBottom | Gets or sets whether the report footer is printed at the bottom of each page, or only after the report content. |
Link.ReportFooterStyle | Gets or sets the style applied to the footer of the printed report. |
Link.ReportFooterText | Gets or sets the text of the report’s footer. |
Link.ReportHeaderStyle | Gets or sets the style applied to a header of the printed report. |
Link.ReportHeaderText | Gets or sets the text of the report’s header. |
Link.TopMarginStyle | Gets or sets the style applied to the top margin of the printed document. |
Link.TopMarginText | Gets or sets the text of the document’s top margin. |
Link.VerticalContentSplitting | Gets or sets whether content bricks outside the right page margin should be split across pages or moved to the next page. |
The following code sample specifies the text, font size, padding, and foreground color of report page headers:
<Page ...
xmlns:dxg="using:DevExpress.WinUI.Grid">
<Grid RowDefinitions="Auto,*">
<!-- ... -->
<>
<dxg:GridControl
x:Name="gridControl" Grid.Row="1"
ItemsSource="{x:Bind ViewModel.Products}" AutoGenerateColumns="False"
ShowTotalSummary="True" PrintTotalSummary="False">
<dxg:GridControl.Columns>
<dxg:GridTextColumn FieldName="ProductName" />
<dxg:GridTextColumn FieldName="Country" />
<dxg:GridTextColumn FieldName="City" />
<dxg:GridTextColumn FieldName="Quantity">
<dxg:GridTextColumn.PrintCellStyle>
<dxg:GridPrintBrickStyle FontWeight="Bold" Background="LightSeaGreen"/>
</dxg:GridTextColumn.PrintCellStyle>
</dxg:GridTextColumn>
<dxg:GridTextColumn FieldName="UnitPrice" />
</dxg:GridControl.Columns>
<dxg:GridControl.TotalSummary>
<dxg:GridTotalSummaryItem FieldName="Quantity" SummaryType="Sum" />
</dxg:GridControl.TotalSummary>
</dxg:GridControl>
</Grid>
</Page>
using DevExpress.WinUI.Grid.Printing;
using DevExpress.WinUI.Printing;
private void HyperlinkButton_Click(object sender, RoutedEventArgs e) {
var documentSource = new PrintableLink(new GridControlPrinter(gridControl));
// Customize the document's print appearance
documentSource.PageHeaderText = "Invoices";
documentSource.PageHeaderStyle = new PrintBrickStyle {
FontSize = 20.0d,
Background = Colors.LightGreen,
Padding = new Thickness(10,0,0,0),
BorderColor = Colors.LightGray,
BorderThickness = 1.0d
};
documentSource.ReportFooterText = "Invoice revision date: 1st of October";
documentSource.ReportFooterStyle = new PrintBrickStyle {
FontSize = 14.0d,
FontStyle = FontStyle.Oblique,
Foreground = Colors.DarkGray,
Padding = new Thickness(0, 60, 0, 0)
};
// Assign the customized document to the DocumentViewer control
documentViewer.DocumentSource = documentSource;
}