wpf - 访问 WPF 中的 SQL Server 报表查看器控件

标签 wpf reportviewer

我尝试过在WPF中使用VS2010自带的报表查看器。我创建了该报告并使用它有效的 Windows 应用程序对其进行了测试。然后我在 WPF 中使用相同的 RDLC 文件和相同的参数集,但失败了。我使用的代码如下 MainWindow.xaml.cs

public MainWindow()
{
   ObjectModel DataObject = new ObjectModel();
   DataObject.SetEPSDetails();
   WindowsFormsHost host = new WindowsFormsHost();
   ReportViewer RptViewer = new ReportViewer();
   host.Child = RptViewer;
   Grid HostGrid = this.FindName("GrdRow") as Grid;
   HostGrid.Children.Add(host);

   RptViewer.ProcessingMode = ProcessingMode.Local;
   RptViewer.LocalReport.ReportEmbeddedResource = "RDLWPF.PrintPreview.rdlc";

   RptViewer.LocalReport.SetParameters(new Microsoft.Reporting.WinForms.ReportParameter("Payer", DataObject.Payer));
   RptViewer.LocalReport.SetParameters(new Microsoft.Reporting.WinForms.ReportParameter("ValueDate", DataObject.ValueDate));
   RptViewer.LocalReport.SetParameters(new Microsoft.Reporting.WinForms.ReportParameter("FileName", DataObject.FileName));
   RptViewer.LocalReport.SetParameters(new Microsoft.Reporting.WinForms.ReportParameter("Description", DataObject.Description));
   RptViewer.LocalReport.SetParameters(new Microsoft.Reporting.WinForms.ReportParameter("PrintedDate", DataObject.PrintDate));
   RptViewer.LocalReport.SetParameters(new Microsoft.Reporting.WinForms.ReportParameter("FileLastUpdated", DataObject.FileLastUpdated));
   RptViewer.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("FileDetails", DataObject.EPSDetails));
   RptViewer.RefreshReport();
}

Xaml如下Mainwindow.Xaml

<Window x:Class="RDLWPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:viewer="clr-namespace:Microsoft.Reporting.WinForms;assembly=Microsoft.ReportViewer.WinForms"
        Title="MainWindow" Height="350" Width="525"  AllowsTransparency="False" >
    <Grid x:Name="GrdRow">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
        </Grid.RowDefinitions>

    </Grid>

我刚刚得到空的 XAML 窗口。报告控件也不显示。我知道我做错了,但找不到它是什么。有谁可以帮我纠正这个问题吗?另外,如果有人可以向我发送 MVVM 方法中的代码示例,那也很棒

感谢和问候 文卡特什

最佳答案

以下是我为使 SQL 报表查看器出现在我的 WPF 应用程序中所做的操作:

1- 添加对 WindowsFormsIntegation 的项目引用
2- 添加对 Microsoft.ReportViewer.Winforms 的项目引用
3- 添加对 Microsoft.ReportViewer.Common 的项目引用

4- 在我的 View 模型中定义 WindowsFormsHost

public MainWindowViewModel()
{
   ReportViewer reportViewer = new ReportViewer();
   _viewer.Child = reportViewer;
}

private WindowsFormsHost _viewer = new WindowsFormsHost();
public WindowsFormsHost Viewer
{
  get
  {
    return _viewer;
  }
  set
  {
    _viewer = value;
    NotifyPropertyChanged("Viewer");
  }
}

4-从 View 绑定(bind)对 View 模型的引用

<Grid>
  <Grid.RowDefinitions>
    <RowDefinition/>
  </Grid.RowDefinitions>
  <ContentPresenter Grid.Row="0" Content="{Binding Viewer}" />
</Grid>

关于wpf - 访问 WPF 中的 SQL Server 报表查看器控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4526635/

相关文章:

asp.net - 分页在报表查看器中不起作用

wpf - VisualStateManager 中 MouseOver 和 PointerOver 的区别

iframe - 报告加载时的 ReportViewerForMvc 事件

c# - 在 WPF 应用程序中使用远程数据库的建议方法是什么

c# - 我如何让我的数据网格随窗口增长并保持利润率?

c# - 在设计模式下使用 visual studio 2012 中的报表查看器时出错

c# - Microsoft 报表查看器对象

c# - 对 CSV 报告的 URI 访问

c# - 确保所有线程都关闭或强行关闭一个线程?

c# - 如何使用代码隐藏从 wpf C# 中的数据网格获取所有标题文本