【问题标题】:WPF with a report viewer binding issues带有报表查看器绑定问题的 WPF
【发布时间】:2016-09-18 01:03:27
【问题描述】:

我继承了一个使用 MVVM Light 以 WPF MVVM 样式完成的程序。我需要从从 SQL 服务器拉入 ViewModel 的数据中从程序中出来的报告。如果不需要,我并不担心坚持使用 MVVM,我只需要它工作即可。

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

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

在我拥有的 DemoForm_Load 上

this.reportViewer1.RefreshReport();

我觉得我在某个地方缺少数据绑定。当我尝试拉出报告时,它只有三个字段的标题,我试图在 rdlc 的表中填充。我想我需要在打开新表单时设置报表的数据源,只是不确定如何设置。

【问题讨论】:

    标签: c# wpf data-binding report rdlc


    【解决方案1】:

    这是一个简单的例子。

    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(截图)

    运行时截图

    【讨论】:

    • 我将此部分添加到 Form_Load 但不确定放在这里的内容 - reportDataSource1.Value = dataset; /* 在这里使用你的数据源 */ 我的源是一个 viewModel 对象
    • 在您的报表中,您配置了特定类型的数据集“DataSet1”。将值设置为该类型的实例。您在问题中提到了“CalibrationViewModel”,所以我想这就是一个例子。
    • 当我使用 CalibrationViewModel1 作为值时,我收到一条错误消息,指出它在当前上下文中不存在。这就是我放入 form_load Microsoft.Reporting.WinForms.ReportDataSource reportDataSource1 = new Microsoft.Reporting.WinForms.ReportDataSource(); reportDataSource1.Name = "DataSet2"; reportDataSource1.Value = CalibrationViewModel1; reportViewer1.LocalReport.DataSources.Add(reportDataSource1); reportViewer1.RefreshReport();
    • 试试:reportDataSource1.Value = new List() { CalibrationViewModel1 };
    • 仍然得到一个在当前上下文中不存在的
    猜你喜欢
    • 1970-01-01
    • 2012-12-16
    • 1970-01-01
    • 2014-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-14
    相关资源
    最近更新 更多