【发布时间】:2015-10-16 10:48:02
【问题描述】:
我有一个包含许多不同表单的 MDI 应用程序。然后我在 ReportForm 中有一个 ReportViewer 控件,我在其中动态加载 RDLC 报告。
ReportForm 通过单击按钮从其他表单打开。
在ReportForm 内部,ReportViewer 控件上方有一个ComboBox,其中包含该特定表单的可用报告列表。该列表是根据打开 ReportViewer 的表单动态生成的。
该列表是通过在 Reports 项目(其输出是一个 DLL)中查找与表单相关的文件夹生成的,该文件夹包含相应的 .rdlc 文件。
问题是这样的:
既然 RDLC 都已经通过报表设计器生成和配置并绑定到特定的 DataSet 和 DataTable,为什么我每次都需要重新创建 ReportDataSource 对象(DataSet、DataTable)并将其重新绑定到报表?强>
我的实际代码:
// Code used to open a new ReportForm Window
private void bindingNavigatorViewReport_Click(object sender, EventArgs e)
{
// If the form is already open, then Focus on it
if (MdiMain.IsFormOpen(ApplicationForm.ReportForm))
{
Application.OpenForms[formName].Focus();
}
// I need to instantiate a new ReportForm
else
{
/* The parameters are:
* - ApplicationForm enum which identifies the Form;
* - The Form corresponding DataSet
*/
ReportForm reportForm = new ReportForm(ApplicationForm.SchoolForm, SchoolDataSet)
{
MdiParent = MdiParent,
Icon = Resources.ViewReportIcon
};
reportForm.Show();
}
}
// Code used to load report
/* In the ReportForm class I have a method that casts the parameters to the
* specific types. So I can obtain the DataSet to use.
*/
// Report Path inside DLL assembly taken from SelectedValue of ComboBox
string reportSource = cmbReport.SelectedValue.ToString();
// Get report Stream from DLL Path
Stream reportStream = assembly.GetManifestResourceStream(reportSource);
// Load report Stream
reportViewer1.LocalReport.LoadReportDefinition(reportStream);
/* Create a new DataSource with DataSet name and DataTable object
* The DataTable object must come from DataSet, ie: DataSet.SchoolListDataTable.
*/
ReportDataSource rds = new ReportDataSource("DataSetName", DataTableObject);
reportViewer1.LocalReport.DataSources.Clear();
reportViewer1.LocalReport.DataSources.Add(rds);
reportViewer1.LocalReport.Refresh();
主要问题是我没有“动态方式”从表单中获取与表单关联的每个报表的 DataTable 对象,因为当我单击“打开报表窗口”按钮时,我有一个报表列表等等 DataSet(与该表单相关联),但有许多不同的 DataTables,而不仅仅是一个。
** 更新:最终解决方案 **
最后我发现,当我加载报告流时,我可以使用这段代码:
// Loads all the data sources associated to the current selected report
IEnumerable<string> dsNames = reportViewer1.LocalReport.GetDataSourceNames();
foreach (string dsName in dsNames)
{
// dsName is equal to the string "V_REP_SCHOOLS" that is the corresponding DataTable name
ReportDataSource rds = new ReportDataSource(dsName, _currentReportDataSet.Tables[dsName]);
reportViewer1.LocalReport.DataSources.Add(rds);
}
加载报告流后(查看上面的代码),GetDataSourceNames() 方法为我提供了在报告。
所以我将我的 DataSet 命名为我需要从中加载数据的 DataTable,例如“V_REPORT_SCHOOLS”,然后我从我的 DataSet 中按名称加载了 DataTable。
【问题讨论】:
-
你想让哪个对象动态化?举个例子。
-
对于每种不同形式的 MDI,我都有一个或多个 .rdlc 报告。
DataSet对于特定表单的每个报告都是相同的,但DataTables是不同的。因此,当我调用 ReportForm 时,我可以将DataSet类(XSD 模式)作为参数传递,但不能传递特定的DataTable,因为有很多。我需要它。请再次查看代码更新问题。
标签: c# winforms reporting-services datatable datasource