嵌入水晶报表页脚中的子报表没有显示数据
问题描述:
我有一个水晶报表,里面嵌入了一个子报表。这些报告单独运行正常,但是当我一起运行时,我没有在子报表中获取数据。我已经验证了数据存在于我的2个数据集中,并且我获得了主要报告的数据,但没有获得数据。我错过了什么? (VS 2008 4.0 .NET Framework)嵌入水晶报表页脚中的子报表没有显示数据
// check whether the dataset is manual; if so add the subreport queries
if (AARpt.HasSubreports)
{
string srName;
string sProc;
using (AAData dc = new AAData(AAApp, true))
{
for (int i = 0; i < cRpt.ReportDefinition.ReportObjects.Count; i++)
{
ReportObject CurrentObject = cRpt.ReportDefinition.ReportObjects[i];
if (CurrentObject.Kind == CrystalDecisions.Shared.ReportObjectKind.SubreportObject)
{
SubreportObject sr = (SubreportObject)CurrentObject;
rd = sr.OpenSubreport(sr.SubreportName);
srName = sr.SubreportName;
if (AARpt.SubReps.TryGetValue(srName, out sProc))
{
dsSubRept = dc.LoadSet(sProc);
if (dsSubRept != null)
{
DataTable dt1 = ds.Tables["MyTable"];
rd.SetDataSource(dt1);
rd.VerifyDatabase();
}
else
{
MessageBox.Show("We're sorry, but an error was encountered attempting to create this report","Utility", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return false;
}
}
}
}
}
}
public DataSet LoadSet(SqlCommand _cmd)
{
// returns table "MyTable"
SqlDataAdapter DA;
DataSet DS = new DataSet();
_cmd.CommandType = CommandType.StoredProcedure;
_cmd.Connection = _Conn;
DA = new SqlDataAdapter(_cmd);
DA.Fill(DS, "MyTable");
return DS;
}
答
我使用了错误的数据集的子报表....否则它是完美的。更新下面的代码。
// check whether the dataset is manual; if so add the subreport queries
if (AARpt.HasSubreports)
{
string srName;
string sProc;
using (AAData dc = new AAData(AAApp, true))
{
for (int i = 0; i < cRpt.ReportDefinition.ReportObjects.Count; i++)
{
ReportObject CurrentObject = cRpt.ReportDefinition.ReportObjects[i];
if (CurrentObject.Kind == CrystalDecisions.Shared.ReportObjectKind.SubreportObject)
{
SubreportObject sr = (SubreportObject)CurrentObject;
rd = sr.OpenSubreport(sr.SubreportName);
srName = sr.SubreportName;
if (AARpt.SubReps.TryGetValue(srName, out sProc))
{
dsSubRept = dc.LoadSet(sProc);
if (dsSubRept != null)
{
// Following line added (not sure if needed, but was suggested in several spots)
rd.DataSourceConnections.Clear();
// The datasource in the following line changed - this was my real issue
DataTable dt1 = dsSubRept.Tables["MyTable"];
rd.SetDataSource(dt1);
rd.VerifyDatabase();
}
else
{
MessageBox.Show("We're sorry, but an error was encountered attempting to create this report","Utility", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return false;
}
}
}
}
}
}
// The following is part of a class for loading data, but it is straightforward otherwise
// I just included so you know that it returns a DataSet with a table named "MyTable"
public DataSet LoadSet(SqlCommand _cmd)
{
// returns table "MyTable"
SqlDataAdapter DA;
DataSet DS = new DataSet();
_cmd.CommandType = CommandType.StoredProcedure;
_cmd.Connection = _Conn;
DA = new SqlDataAdapter(_cmd);
DA.Fill(DS, "MyTable");
return DS;
}