gpt4 book ai didi

c# - 为本地报告设置数据源 - .NET 和报告查看器

转载 作者:可可西里 更新时间:2023-11-01 08:54:52 25 4
gpt4 key购买 nike

我创建了一个自定义控件(带有报表查看器的窗口窗体)。我有以下代码来加载本地报告:

包含在 CustomReportViewer 类中

//Load local report 
this.reportViewer1.ProcessingMode = ProcessingMode.Local;
//enable loading of external images
this.reportViewer1.LocalReport.EnableExternalImages = true;
//pass the report to the viewer
using (FileStream stream = new FileStream(filename, FileMode.Open))
{
this.reportViewer1.LocalReport.LoadReportDefinition(stream);
}

我这样调用它:

CustomReportViewer reportViewer = new CustomReportViewer();

这工作正常,出现一个包含报表查看器控件的窗口窗体但是我收到以下消息:

A data source instance has not been supplied for the data source "ReportData"

我不太确定如何设置数据源?我需要的数据存储在远程数据库中...我需要做什么才能建立此连接?

最佳答案

您需要创建一个 ReportDataSource ,并设置其 Value 属性 - 例如DataTableIEnumerablesupported sources

举个例子,假设存在一个返回 DataSet 的方法,使用一个 DataTable 匹配您的报告所需的列:

DataSet ds = SomeMethodToRetrieveDataSet(); // e.g. via DataAdapter
// If your report needs parameters, they need to be set ...
ReportParameter[] parameters = new ReportParameter[...];

ReportDataSource reportDataSource = new ReportDataSource();
// Must match the DataSource in the RDLC
reportDataSource.Name = "ReportData";
reportDataSource.Value = ds.Tables[0];

// Add any parameters to the collection
reportViewer1.LocalReport.SetParameters(parameters);
reportViewer1.LocalReport.DataSources.Add(reportDataSource);
reportViewer1.DataBind();

请注意,将 RDLC 嵌入到程序集中通常比必须保留单独的 RDLC 文件更容易。为此,请在 RDLC 上选择 Build Action 作为 Embedded Resource,然后您可以设置 ReportEmbeddedResource 属性:

reportViewer1.LocalReport.ReportEmbeddedResource = 
"MyOrganisation.MyAssembly.NameSpace.MyReportName.rdlc";

请注意,资源字符串必须包含资源的完全限定名称(包括程序集)。

关于c# - 为本地报告设置数据源 - .NET 和报告查看器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8802707/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com