How to created filtered reports in WPF?
- by Michael Goyote
Creating reports in WPF.
I have two related tables.
Table A-Customer:
CustomerID(PK)
Names
Phone Number
Customer Num
Table B-Items:
Products
Price
CustomerID
I want to be able to generate a report like this:
CustomerA
Items Price
Item A 10
Item B 10
Item C 10
---------------
Total 30
So this is what I have done:
<Window x:Class="ReportViewerWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:rv="clr-namespace:Microsoft.Reporting.WinForms;
assembly=Microsoft.ReportViewer.WinForms"
Title="Customer Report" Height="300" Width="400">
<Grid>
<WindowsFormsHost Name="windowsFormsHost1">
<rv:ReportViewer x:Name="reportViewer1"/>
</WindowsFormsHost>
</Grid>
Then I created a dataset and loaded the two tables, followed by a report wizard (dragged all the available fields and dropped them to the Values pane).
The code behind the WPF window is this:
public partial class CustomerReport : Window
{
public CustomerReport()
{
InitializeComponent();
_reportViewer.Load += ReportViewer_Load;
}
private bool _isReportViewerLoaded;
private void ReportViewer_Load(object sender, EventArgs e)
{
if (!_isReportViewerLoaded)
{
Microsoft.Reporting.WinForms.ReportDataSource reportDataSource1 = new Microsoft.Reporting.WinForms.ReportDataSource();
HM2DataSet dataset = new HM2DataSet();
dataset.BeginInit();
reportDataSource1.Name = "DataSet";//This is the dataset name
reportDataSource1.Value = dataset.CustomerTable;
this.reportViewer1.LocalReport.DataSources.Add(reportDataSource1);
this.reportViewer1.LocalReport.ReportPath = "../../Report3.rdlc";
dataset.EndInit();
HM2DataSetTableAdapters.CustomerTableAdapter funcTableAdapter = new HM2DataSetTableAdapters.CustomerTableAdapter();
funcTableAdapter.ClearBeforeFill = true;
funcTableAdapter.Fill(dataset.CustomerTable);
_reportViewer.RefreshReport();
_isReportViewerLoaded = true;
}
}
As you might have guessed this loaded this list of customer with items and price:
Customer Items Price
Customer A Items A 10
Customer A Items B 10
Customer B Items D 10
Customer B Items C 10
How can I fine-tune this report to look like the one above, where the user can filter the customer he wants displayed on the report? Thanks in advance for the help. I would have preferred to use LINQ whenever filtering data