Charlie Salts
Charlie Salts

Reputation: 13488

Crystal Reports using Report and SubReport

Suppose I have the following:

public class MyObject
{
   public string Name {get; set;}
   public List<MySubObject> SubObjects {get; set;}
}

public class MySubObject
{
   public string SubName {get; set;}
}

Is there a way I can set up a report and sub report to show a list of MyObjects (each with a list of subreports for their respective MySubObjects?

Upvotes: 1

Views: 1679

Answers (1)

Lee Tickett
Lee Tickett

Reputation: 6027

I think your class definition is missing the name of the List<MySubObject>:

public class MyObject
{
   public string Name {get; set;}
   public List<MySubObject> SubObjects {get; set;}
}

public class MySubObject
{
   public string SubName {get; set;}
}

You can loop through and populate a dataset (you don't really need to use subreports, uness your data is more complex than you've described above):

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);
 }
}

Then point your crystal report datasource at the dataset:

using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
CrystalReport1 objRpt = new CrystalReport1();
objRpt.SetDataSource(ds.Tables[1]);
crystalReportViewer1.ReportSource = objRpt;
crystalReportViewer1.Refresh(); 

Upvotes: 1

Related Questions