WPF与报表查看器绑定问题

时间:2016-05-20 17:07:44

标签: c# wpf data-binding report rdlc

我继承了一个用MVVM Light以WPF MVVM风格完成的程序。我需要从SQL服务器中导入到ViewModels的数据中提出报告。如果我不需要,我并不担心坚持使用MVVM,我只是需要它才能工作。

我正在尝试使用已创建的ViewModel作为报告的数据。到目前为止,我已经创建了一个名为 Report1.rdlc 的报告,该报告使用数据源 CalibrationViewModel ,数据集 DataSet1

我有一个名为 DemoForm 的表单,它会在主WPF页面上点击按钮打开一个新窗口,其中包含一个报表查看器( reportViewer1 )。那用于显示报告。 reportViewer绑定到 CalibrationViewModelBindingSource

在DemoForm_Load上我有

this.reportViewer1.RefreshReport();

我觉得我错过了一个数据绑定。当我尝试提取报告时,它只有我尝试在rdlc上的表中填充的三个字段的标题。我想在打开新表单时我需要设置报表的DataSource,而不知道如何使用。

1 个答案:

答案 0 :(得分:0)

这是一个简单的例子。

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(截图) enter image description here

运行时截图 enter image description here