假设我有以下内容:
public class MyObject
{
public string Name {get; set;}
public List<MySubObject> SubObjects {get; set;}
}
public class MySubObject
{
public string SubName {get; set;}
}
我是否可以设置报告和子报告以显示MyObjects
列表(每个列表都包含各自MySubObjects
的子报告列表?
答案 0 :(得分:1)
我认为您的班级定义缺少List<MySubObject>
的名称:
public class MyObject
{
public string Name {get; set;}
public List<MySubObject> SubObjects {get; set;}
}
public class MySubObject
{
public string SubName {get; set;}
}
您可以遍历并填充数据集(您实际上不需要使用子报表,您的数据比上面描述的更复杂):
DataSet ds = new DataSet();
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("SubReport");
foreach (MyObject myo in list_of_myobjects)
{
foreach (MySubObject myso in myo.SubObjects)
{
DataRow dr = dt.NewRow();
dr[0] = myo.Name;
dr[1] = myso.SubName;
dt.Rows.Add();
ds.Tables.Add(dt);
}
}
然后将晶体报告数据源指向数据集:
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
CrystalReport1 objRpt = new CrystalReport1();
objRpt.SetDataSource(ds.Tables[1]);
crystalReportViewer1.ReportSource = objRpt;
crystalReportViewer1.Refresh();