Version

Adding WPF Reporting to Your Page

Before You Begin

Before you can preview, print, or export a report, you must instantiate a Report object and add EmbeddedVisualReportSection objects to its Sections collection. After adding sections to the Report object, you can display a print preview of the report using the xamReportPreview™ control, export the report in XML paper specification (XPS), or print the report to a printer.

What You Will Accomplish

You will create a report using xamDataGrid™ as the main content.

You will add xamReportPreview to your window.

Follow these Steps to create the report

  1. Create a Microsoft® Windows® Presentation Foundation Window project.

  2. In the Solution Explorer, add the following NuGet package references to your project. For more information on setting up the NuGet feed and adding NuGet packages, you can take a look at the following documentation: NuGet Feeds.

    • Infragistics.WPF.Reporting

    • Infragistics.WPF.DataGrids

You only need a reference to the first package in the list above to implement reporting functionality in your application. The XamDataGrid control is going to be used to demonstrate reporting functionality in the rest of this topic.

  1. Add a namespace declaration for XamDataGrid inside the opening Window tag.

In XAML:

xmlns:igDP="http://infragistics.com/DataPresenter"
  1. Add a DockPanel container to the Window.

In XAML:

<DockPanel>
    <!--TODO: Add a Button here-->
    <!--TODO: Add xamDataGrid here-->
</DockPanel>
  1. Add a Button control to the DockPanel container.

    1. Set the attached DockPanel.Dock property to Top.

    2. Set the Content property to "Print xamDataGrid".

    3. Attach an event handler to the Button control’s Click event.

In XAML:

<Button
    DockPanel.Dock="Top"
    Content="Print xamDataGrid"
    Click="Button_Click" />
  1. Add xamDataGrid to the DockPanel container.

    1. Set the Name property so you can reference it in the code-behind.

    2. Set the BindToSampleData to True.

In XAML:

<igDP:XamDataGrid Name="xamDataGrid1" BindToSampleData="True" />
  1. Open the code-behind and place using/Imports directives in your code-behind so you don’t have to type out a member’s fully qualified name.

In Visual Basic:

Imports Infragistics.Windows.Reporting

In C#:

using Infragistics.Windows.Reporting;
  1. Add an event handler for the Button control’s Click event if a method stub has not been created for you, instantiate a Report and an EmbeddedVisualReportSection object and pass a reference to the xamDataGrid control to its constructor. Finally add the EmbeddedVisualReportSection object to the Report object’s Sections collection and invoke the Report object’s Print or Export method.

In Visual Basic:

Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
    Dim report1 As New Report()
    Dim section1 As New EmbeddedVisualReportSection(Me.xamDataGrid1)
    report1.Sections.Add(section1)
    report1.Print()
    'The following line of code will export
    'the contents of xamDataGrid to an XPS document.
    'report1.Export(OutputFormat.XPS, "c:\\xamDataGrid1.xps")
End Sub

In C#:

private void Button_Click(object sender, RoutedEventArgs e)
{
    Report report1 = new Report();
    EmbeddedVisualReportSection section1 = new EmbeddedVisualReportSection(this.xamDataGrid1);
    report1.Sections.Add(section1);
    report1.Print();
    //The following line of code will export
    //the contents of xamDataGrid to an XPS document.
    //report1.Export(OutputFormat.XPS, "c:\\xamDataGrid1.xps");
}
  1. Run the project.

You should see a Window that looks similar to the screen shot below. You can click the button labeled "Print xamDataGrid" to print the contents of the xamDataGrid control.

WPF Reporting Creating a Report 01.png

Follow these Steps to add xamReportPreview to your window

  1. Create a Microsoft® Windows® Presentation Foundation Window project.

  2. Add the following NuGet package reference to your project:

    • Infragistics.WPF.Reporting

    For more information on setting up the NuGet feed and adding NuGet packages, you can take a look at the following documentation: NuGet Feeds.

  1. Add the Infragistics name space declarations

In XAML:

xmlns:igReporting="http://infragistics.com/Reporting"

In Visual Basic:

Imports Infragistics.Windows.Reporting

In C#:

using Infragistics.Windows.Reporting;
  1. Name the default Grid layout container so you can reference it in the code-behind.

In XAML:

<Grid Name="layoutRoot">
</Grid>
  1. Attach an event handler to the Window’s Loaded event if you are going to use the code-behind.

In XAML:

<Window ... Loaded="Window_Loaded">
  1. Create an instance of xamReportPreview control and add it in the main Grid.

In XAML:

<igReporting:XamReportPreview Name="xamReportPreview1" />

In Visual Basic:

Private Sub Window_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
    Dim xamReportPreview1 = New XamReportPreview()
    Me.layoutRoot.Children.Add(xamReportPreview1)
End Sub

In C#:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    XamReportPreview xamReportPreview1 = new XamReportPreview();
    this.layoutRoot.Children.Add(xamReportPreview1);
}
  1. Run your project.

When you run the project, you should see a Window that looks similar to the screen shot below. The xamReportPreview control will not display any content since it has not generated a preview of a report. You have to call xamReportPreview’s GeneratePreview method and pass in a reference to a Report object.

WPF Reporting Adding xamReportPreview to a Window Using Procedural Code 01.png