gpt4 book ai didi

winforms - 如何为具有 EF 数据源的多个报表重用一个 ReportViewer 控件?

转载 作者:行者123 更新时间:2023-12-04 11:41:13 25 4
gpt4 key购买 nike

我想坚持使用一个单一的表格和 ReportViewer控制,并在运行时分配各种报告和数据源。快速谷歌检查所揭示的多样化和复杂的解决方案促使我宁愿在这里问。我怎样才能做到这一点?

最佳答案

您将需要一个具有 ReportViewer 控件、RDLC 报告和数据源的表单;可能有几种实现方法,但您可以使用“报告管理器”类公开方法,每个方法显示特定报告(例如 ShowOrdersReport()ShowTimeSheetReport() 等) - 或者您可以使用 ReportBase 方法定义基 Show() 类在需要时提示输入参数的实现......无论什么工作,它基本上归结为:

var reportForm = new ReportViewerForm(); // instantiate the form
// load the report definition into the viewer:
reportForm.reportViewer1.LocalReport.ReportEmbeddedResource = "Namespace.Report.rdlc";

// loading the data works great with a DataSet:
var data = _reportingDataSet.Tables[0];
// create the ReportDataSource with the report data:
var reportDataSource = new ReportDataSource("rdsReportDataSource", data);

reportForm.ShowReport(new[] { reportDataSource });

在这里,您需要注入(inject) _reportingDataSet 依赖项;如果您使用参数化存储过程,您将需要在填充数据集之前提示输入报告参数。
ShowReport() 方法将数据源添加到 LocalReport.DataSources ,然后调用 RefreshReport() 显示您指定的报告。

public void ShowReport(IEnumerable<ReportDataSource> dataSources)
{
foreach (var dataSource in dataSources)
reportViewer1.LocalReport.DataSources.Add(dataSource);

reportViewer1.RefreshReport();
}

关于winforms - 如何为具有 EF 数据源的多个报表重用一个 ReportViewer 控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12584419/

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