0
votes

I inheritated a program that was done in WPF MVVM style with MVVM Light. I need reports to come out of the program from data that was pulled in from a SQL server into ViewModels. I am not worried about sticking to MVVM if I dont have to, I just need it to work.

I am trying to use one of the ViewModel's already created as my data for the report. So far I have created a report named Report1.rdlc that is using the datasource CalibrationViewModel with dataset DataSet1

I have a form called DemoForm that opens a new window on a button click from the main WPF page with a report viewer (reportViewer1) inside of it. That is used to display the report. The reportViewer is bound to CalibrationViewModelBindingSource.

On the DemoForm_Load i have

this.reportViewer1.RefreshReport();

I feel like somewhere im missing a databinding. When I try to pull up the report it just has the header for the three fields I tried to populate in a table on the rdlc. I think I need to set the DataSource of the report when I open the new form just unsure of how to.

1

1 Answers

0
votes

Here is a simple example.

XAML

<Window x:Class="WpfApplication55.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication55"
        xmlns:rv="clr-namespace:Microsoft.Reporting.WinForms;assembly=Microsoft.ReportViewer.WinForms"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <WindowsFormsHost>
            <rv:ReportViewer x:Name="reportViewer1" />
        </WindowsFormsHost>
    </Grid>
</Window>

CS

using System.Collections.Generic;
using System.Windows;

namespace WpfApplication55
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            reportViewer1.LocalReport.ReportEmbeddedResource = "WpfApplication55.Report1.rdlc";

            Microsoft.Reporting.WinForms.ReportDataSource reportDataSource1 = new Microsoft.Reporting.WinForms.ReportDataSource();
            reportDataSource1.Name = "DataSet1";
            reportDataSource1.Value = new List<MyReportData>() { new MyReportData() { Field1 = "Value 1", Field2 = "Value 2" } };
            reportViewer1.LocalReport.DataSources.Add(reportDataSource1);
            reportViewer1.RefreshReport();
        }
    }

    public class MyReportData
    {
        public string Field1 { get; set; }
        public string Field2 { get; set; }
    }
}

RDLC (screenshot) enter image description here

Runtime screenshot enter image description here