【问题标题】:How do I get data from a dataset to show in a ReportViewer?如何从数据集中获取数据以显示在 ReportViewer 中?
【发布时间】:2016-09-01 01:32:24
【问题描述】:

我在 WindowsFormHost 中创建了一个 ReportViewer。这是我的 XAML 代码。

<Grid x:Name="grdPrintingGrid" Visibility="Visible" Height="440" Margin="105, 0, 0 0" VerticalAlignment="Top" Grid.ColumnSpan="2" >
        <TextBox x:Name="txtPRTTitle" IsReadOnly="True" HorizontalAlignment="Left" Height="32" Margin="10,4,0,0" VerticalAlignment="Top" Width="450" FontFamily="Arial" FontSize="18.667" Background="#FFE8F9FF" BorderThickness="0"/>
        <WindowsFormsHost x:Name="wfhFormsHost" Visibility="Hidden" HorizontalAlignment="Left" Height="312" Margin="10,54,0,0" VerticalAlignment="Top" Width="683">
            <rv:ReportViewer x:Name="rvWeeklyList" />
        </WindowsFormsHost>

        <Grid x:Name="grdPWLGrid" Visibility="Visible" Height="440" Margin="0, 0, 0 0" VerticalAlignment="Top">
            <DataGrid IsReadOnly="True" Name="dgPWLCGrid" ScrollViewer.HorizontalScrollBarVisibility="Hidden" Background="#FFE8F9FF" HorizontalAlignment="Left" VerticalAlignment="Top" Height="255" Margin="10,62,0,0" Width="693" BorderThickness="1" BorderBrush="Black" CanUserResizeRows="False" >
            </DataGrid>

            <Button x:Name="btnPWLPrint" Content="Print" HorizontalAlignment="Left" Height="26" Margin="307,388,0,0" VerticalAlignment="Top" Width="74" FontFamily="Arial" FontSize="14.667" Background="#FFEEFFFF" Click="btnPWLPrint_Click"/>
        </Grid>
    </Grid>

我正在我的数据库中调用一个存储过程来获取数据。我使用报告向导来创建我的报告 rdlc。我称它为PrintWeeklyList.rdlc。对于选择数据源类型,我选择了数据库。对于选择数据库模型,我选择数据集。对于数据库对象,我选择了一个名为GetPrintWeeklyListData 的存储过程。我将数据源命名为 PrintWeeklyListDataSet。我将 DataSet 命名为 PrintWeeklyListDS

这是我加载 ReportViewer 的 C# 代码:

private void RVWeeklyList_Load(object sender, EventArgs e)
{
        if (!isReportViewerLoaded)
        {
            ReportViewer rvWeeklyList = new ReportViewer();

            Microsoft.Reporting.WinForms.ReportDataSource reportDataSource1 =
                new Microsoft.Reporting.WinForms.ReportDataSource();

            PrintWeeklyListDataSet dataset = new PrintWeeklyListDataSet();

            dataset.BeginInit();

            reportDataSource1.Name = "PrintWeeklyListDS"; //Name of the report dataset in our .RDLC file
            reportDataSource1.Value = dataset.GetPrintWeeklyListData;
            rvWeeklyList.LocalReport.DataSources.Add(reportDataSource1);

            //rvWeeklyList.ServerReport.GetDataSources();
            rvWeeklyList.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local;
            rvWeeklyList.LocalReport.ReportEmbeddedResource = "PrintWeeklyList.rdlc";

            dataset.EndInit();
            PrintWeeklyListDataSetTableAdapters.GetPrintWeeklyListDataTableAdapter pwlTableAdapter =
                new PrintWeeklyListDataSetTableAdapters.GetPrintWeeklyListDataTableAdapter();
            pwlTableAdapter.ClearBeforeFill = true;
            pwlTableAdapter.Fill(dataset.GetPrintWeeklyListData);


            rvWeeklyList.LocalReport.Refresh();
            rvWeeklyList.RefreshReport();


            isReportViewerLoaded = true;
        }
    }

当我单步执行代码时,我看到 dataset 和 rvWeeklyList 都包含来自存储过程的数据。我一直在搜索谷歌,但我无法弄清楚我缺少什么来显示数据。任何代码示例将不胜感激。

【问题讨论】:

    标签: c# wpf dataset reportviewer windowsformshost


    【解决方案1】:

    首先,将WindowsFormsHost改为Visibility="Visible"

    其次,确保PrintWeeklyList.rdlc文件的Build Action设置为Embedded Resource

    现在使用下面的修改后的代码,并确保将 substring&lt;VisualStudioProjectName&gt; 替换为 ReportEmbeddedResource 的 Visual Studio 项目的名称:

    public MainWindow()
    {
        InitializeComponent();
        rvWeeklyList.Load += RVWeeklyList_Load;
    }
    
    private void RVWeeklyList_Load(object sender, EventArgs e)
    {
        if (!isReportViewerLoaded)
        {
            Microsoft.Reporting.WinForms.ReportDataSource reportDataSource1 =
                new Microsoft.Reporting.WinForms.ReportDataSource();
    
            PrintWeeklyListDataSet dataset = new PrintWeeklyListDataSet();
    
            dataset.BeginInit();
    
            reportDataSource1.Name = "PrintWeeklyListDS";
            reportDataSource1.Value = dataset.GetPrintWeeklyListData;
            rvWeeklyList.LocalReport.DataSources.Add(reportDataSource1);
    
            rvWeeklyList.LocalReport.ReportEmbeddedResource = 
               @"<VisualStudioProjectName>.PrintWeeklyList.rdlc";
    
            dataset.EndInit();
    
            PrintWeeklyListDataSetTableAdapters.GetPrintWeeklyListDataTableAdapter pwlTableAdapter =
                new PrintWeeklyListDataSetTableAdapters.GetPrintWeeklyListDataTableAdapter();
            pwlTableAdapter.ClearBeforeFill = true;
            pwlTableAdapter.Fill(dataset.GetPrintWeeklyListData);
    
            rvWeeklyList.RefreshReport();
    
            isReportViewerLoaded = true;
        }
    }
    

    MSDN文章参考:https://msdn.microsoft.com/en-us/library/hh273267.aspx

    【讨论】:

    • #sly -- 我做了你建议的改变。我现在收到此错误,“本地报表处理期间发生错误。尚未指定报表'PrintWeeklyList'的报表定义。对象的引用未设置为对象的实例。”
    • @Cass 你能告诉我 Visual Studio 项目的名称是什么吗?我认为 ReportEmbeddedResource 字符串仍然不正确。如果可能的话,让我知道它的设置。
    • #sly -- 抱歉。我忘了用我的项目名称替换 。一旦我修复它,报告就会显示。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 2023-03-04
    • 1970-01-01
    • 2015-09-27
    • 2015-11-26
    相关资源
    最近更新 更多