我正在努力让子报告正常工作。我正在使用VS 2010.我在主报告中插入了一个“Subreport1”并继续收到错误:子报告“Subreport1”的数据检索失败,位于....
这是我的代码:
public partial class rptEngOrd : Form
{
public rptEngOrd()
{
InitializeComponent();
}
private List<EngOrd> eoNumParam;
private string eonum;
private DataSet dsReport;
private DataSet dsSubReport;
private void rptEngOrd_Load(object sender, EventArgs e)
{
LoadComboBoxes();
}
private void LoadComboBoxes()
{
try
{
eoNumParam = EngOrdDB.GetDistinctEONum();
cboEONum.DataSource = eoNumParam;
cboEONum.SelectedIndex = -1;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, ex.GetType().ToString());
}
}
private void btnPreview_Click(object sender, EventArgs e)
{
try
{
if (cboEONum.SelectedValue.ToString().Equals("zALL"))
{
eonum = "";
}
else
{
eonum = cboEONum.SelectedValue.ToString();
}
//get the data
dsReport = EngOrdDB.GetEngOrdbyEONum(eonum);
//provide local report information to viewer
reportViewer1.LocalReport.ReportEmbeddedResource = "CustomMenu.rptEngOrd.rdlc";
reportViewer1.ProcessingMode = ProcessingMode.Local;
//prepare report data source
ReportDataSource rds = new ReportDataSource();
rds.Name = "dsReport";
rds.Value = dsReport.Tables[0];
reportViewer1.LocalReport.DataSources.Clear();
reportViewer1.LocalReport.DataSources.Add(rds);
//prepare sub report data source
dsSubReport = EngOrdTaskDB.GetEngOrdTaskbyEONum(eonum);
ReportDataSource rdsSub = new ReportDataSource();
rdsSub.Name = "dsSubReport";
rdsSub.Value = dsSubReport.Tables[0];
reportViewer1.LocalReport.DataSources.Add(rdsSub);
reportViewer1.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(srptProcessingEventHandler);
//load the report viewer
reportViewer1.LocalReport.Refresh();
//MessageBox.Show(eonum);
}
catch (Exception ex)
{
//display generic message back to user
MessageBox.Show(ex.Message);
}
finally
{
}
reportViewer1.RefreshReport();
}
private void srptProcessingEventHandler(object sender, SubreportProcessingEventArgs e)
{
ReportDataSource r = reportViewer1.LocalReport.DataSources[1];
e.DataSources.Add(r);
}
}
答案 0 :(得分:5)
我找了一段时间去帮助我查看子报告数据,Reporting against a domain model,您可以在页面中间找到您感兴趣的代码。
顺便说一下,我认为你应该只在事件处理程序中填充和添加数据
private void srptProcessingEventHandler(object sender, SubreportProcessingEventArgs e)
{
dsSubReport = EngOrdTaskDB.GetEngOrdTaskbyEONum(eonum);
e.DataSources.Add(new ReportDataSource “dsSubReport”, dsSubReport.Tables[0]));
}
并从点击事件中删除此代码。
//prepare sub report data source
dsSubReport = EngOrdTaskDB.GetEngOrdTaskbyEONum(eonum);
ReportDataSource rdsSub = new ReportDataSource();
rdsSub.Name = "dsSubReport";
rdsSub.Value = dsSubReport.Tables[0];
reportViewer1.LocalReport.DataSources.Add(rdsSub);
这样我应该工作。希望这有帮助