【发布时间】:2015-10-08 16:19:14
【问题描述】:
我需要在 VS2013 Report Viewer (.net 4.5 C#) winforms 应用程序中创建一个显示多个报表表的报表。报告表需要使用相同的 SQL 存储过程(以填充名为“DataSet”的 dt),只需根据从前一屏幕的列表框提示中选择的项目来改变返回的数据。我正在尝试使用本地报告模式(我的 winforms 应用程序中引用了 .net 程序集)。下面是单个报告应该如何显示的示例以及我尝试用来生成报告的代码。我有两个 rdlc 文件,包含页眉和页脚的初始文件是 ItemsSoldMonthlyReport_Load 方法中链接的文件。另一个 rdlc 作为子报表嵌入在第一个报表文件(在报表设计器中)的详细信息行中。 ItemsSoldMonthlyReport_Load 方法将 itemType、pFromDate 和 pThruDate 参数正确地传递给子报表方法。 subreport 方法是根据应用于 “SoldList” 数据集的项目数启动的,这是正确的。对itemType 参数中的每个项目使用CC_SubreportProcessingEventHandler 中的GetData 方法,成功返回每个表的数据。每个报告表的"DataSet" DataTable 都设置了正确的数据。但是,似乎 "DataSet" DataTable 只能设置一次作为 ReportDataSource。当我尝试将加载了第二组数据的"DataSet"设置为ReportDataSource但第二个表(即:服装)的数据没有显示在最终报告中时,不会出现错误。相反,第一个数据表(即:玩具)在最终报告显示中显示两次。是否可以做我正在尝试的事情,如果可以,我应该采取什么方法?我已经尝试了很多东西并对此进行了很多研究,但我自己找不到答案。如果报表查看器绝对无法生成这样的报表,任何人都可以建议一个不太昂贵的替代方案吗?在此先感谢...
报告应该是什么样子...
我最近(也是最接近的尝试)创建此类报告的代码...
namespace StoreProject_Forms
{
public partial class rptItemsSoldReport : Form
{
int x = 0;
public rptItemsSoldReport()
{
InitializeComponent();
}
public void CC_SubreportProcessingEventHandler(object sender, SubreportProcessingEventArgs e)
{
DataTable dt = new DataTable();
var mainSource = ((LocalReport)sender).DataSources["SoldList"];
var itemTypes = e.Parameters["itemType"].Values;
DateTime sdate = e.Parameters["pFromDate"].Values;
DateTime edate = e.Parameters["pThruDate"].Values;
StoreProject1.StoreDataSetTableAdapters.spGetItemsSoldReportTableAdapter commDt = new StoreProject1.StoreDataSetTableAdapters.spGetItemsSoldReportTableAdapter();
dt.Clear();
dt = commDt.GetData(sdate, edate, itemTypes[x]);
e.DataSources.Add(new ReportDataSource("DataSet1", dt));
x++;
}
public void ItemsSoldMonthlyReport_Load(DateTime startDate, DateTime endDate, ListBox.SelectedObjectCollection itemTypesTxt)
{
startDate = new DateTime(startDate.Year, startDate.Month, 1);
endDate = new DateTime(endDate.Year, endDate.Month, 1);
System.Collections.Generic.List<ReportParameter> paramList = new System.Collections.Generic.List<ReportParameter>();
string itemTypes = null;
reportViewer1.LocalReport.DataSources.Clear();
StoreProject1.StoreDataSetTableAdapters.spGetItemsSoldReportTableAdapter commDt = new StoreProject1.StoreDataSetTableAdapters.spGetItemsSoldReportTableAdapter();
List<string> itemsA = new List<string>();
Dictionary<string, int> allItemTypes = new Dictionary<string, int>();
DataTable ct = new DataTable();
foreach (KeyValuePair<string, int> item in itemTypesTxt)
{
itemsA.Add(item.Key);
allItemTypes.Add(item.Key, item.Value);
}
this.reportViewer1.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(CC_SubreportProcessingEventHandler);
ReportParameter itemParam = new ReportParameter("itemType");
ct = StoreProject1.ItemList.ConvertToDatatable(allItemTypes);
string[] itemsArray = itemsA.ToArray();
ReportDataSource _rsource = new ReportDataSource("SoldList", ct);
reportViewer1.LocalReport.DataSources.Add(_rsource);
itemParam.Values.AddRange(itemsArray);
paramList.Add(itemParam);
string sfdate = startDate.ToString("MM/yyyy");
paramList.Add(new ReportParameter("pFromDate", sfdate, false));
string stdate = endDate.ToString("MM/yyyy");
paramList.Add(new ReportParameter("pThruDate", stdate, false));
reportViewer1.LocalReport.ReportPath = @"C:/StoreProject1/ItemsSoldReport.rdlc";
reportViewer1.LocalReport.SetParameters(paramList);
this.reportViewer1.SetDisplayMode(DisplayMode.PrintLayout);
this.reportViewer1.LocalReport.Refresh();
}
}
}
【问题讨论】:
-
我对你上面提到的DataSet和DataTable感到困惑,但是对于这个简单的报告,为什么不使用列表而不是SubReport?
-
我不确定您是否理解...报告不会总是有两个报告表。报告需要能够创建从 1 到 x 数量(可能多达 100 个)的此类报告表。创建的表的数量取决于用户从他们之前看到的屏幕中的列表框提示中选择了多少对象。报告需要动态生成空白数据集报告表(按所选项目的数量),然后在运行时由上面的代码填充。列表项会完成此操作吗?
标签: c# winforms reporting-services report-viewer2012