C# 从 DLL 中提取报告并将其放入报告查看器中

发布于 2025-01-05 06:59:32 字数 1004 浏览 0 评论 0原文

我的场景是,我正在创建一个 DLL,其中包含我的报告,然后将该 DLL 添加到我的主项目中。我的报告被设置为嵌入式资源,在我的主项目中,我正在执行以下操作以将报告设置到 ReportViewer 中。

Assembly assembly = Assembly.LoadFrom("MyAssembly.dll");
Stream stream = assembly.GetManifestResourceStream("MyAssembly.Reports.MyReport.rdlc");
MyReportViewer.LocalReport.LoadReportDefinition(stream);
MyReportViewer.RefreshReport();

现在,当我这样做时,我知道报表正在加载,因为我可以设置报表参数。如果它根本没有加载,我会抛出一个异常,指出参数不存在或类似的东西。

发生的情况是我的报告将加载所述参数,当我 RefreshReport() 时,我得到了这个。

尚未指定报告定义的来源。

关于为什么会发生这种情况有任何线索吗?我觉得在从 dll 加载报告时我可能会遗漏一些东西。然而,当我在互联网上搜索时,这似乎是正确的解决方案。

附带说明一下,我的 .dll 中有一个表单,我可以很好地调用它并将报告加载到其中。但是,这是一种不同的情况,因为该表单具体知道该报告的位置。

在这种情况下,我有一个不在 .dll 内的 ReportViewer,并且我试图将 .dll 内的 .rdlc 加载到不在 dll 内的 ReportViewer 中。

编辑 最重要的是,如果我使用以下代码,我可以在 dll 中看到我需要的文件。

    string[] resources = assembly.GetManifestResourceNames();

> "MyApp.Controls.Reports.MyCustomReport.rdlc"

My scenario is that I am creating a DLL that contains my reports inside of it and then adding that DLL to my main project. My report is set as an embedded resource, and in my main project I am doing the following to set the report into the ReportViewer.

Assembly assembly = Assembly.LoadFrom("MyAssembly.dll");
Stream stream = assembly.GetManifestResourceStream("MyAssembly.Reports.MyReport.rdlc");
MyReportViewer.LocalReport.LoadReportDefinition(stream);
MyReportViewer.RefreshReport();

Now when I do this, I know that the Report is loading, somewhat, because I can set ReportParameters. If it wasn't loading at all I would be throwing an exception stating the parameter doesn't exist or something to that effect.

Whats happening is that my Report will load said parameters and when I RefreshReport(), I get this.

The source of the report definition has not been specified.

Any clue as to why this would be happening? I feel I might be missing something when it comes to loading reports from a dll. However, when I scoured the Internet it seemed like this was the correct solution.

As a side note, I have a form inside of my .dll that I am calling and loading reports into perfectly fine. However, that is a different case because that form knows specifically where that report is.

In this situation I have a ReportViewer that is not inside the .dll and I am trying to load the .rdlc that's inside the .dll into my ReportViewer that isn't inside the dll.

EDIT
To top it off, if I use the following code I can see the file that I need in the dll.

    string[] resources = assembly.GetManifestResourceNames();

> "MyApp.Controls.Reports.MyCustomReport.rdlc"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

残花月 2025-01-12 06:59:32

程序开头的这段小代码解决了我的问题

 MyReportViewer.Reset();

this little piece of code at the beginning of the procedure solved my problem

 MyReportViewer.Reset();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文