Devexpress 动态创建 ExtraReports 并动态绑定到对象作为数据源

Devexpress dynamic creating ExtraReports and dynamic binding to object as a datasource

我有三个 classes(Employee、EmployeeCard 和 Children)是这样实现的

public class Employee
{
   public Employee()
   {
       Children = new List<Child>();
   }
   public virtual string FirstName { get; set; }
   public virtual string LastName { get; set; }
   public virtual EmployeeCard EmployeeCard { get; set; }
   public virtual IList<Child> Children { get; protected set; }
   public virtual void AddChild(Child child)
   {
       child.Employee = this;
       Children.Add(child);
   }
   public static List<Employee> GetData()
   {
       List<Employee> empList = new List<Employee>();
       for(i=0;i<5;i++)
       {
           Employee emp = new Employee();
           emp.FirstName = "Fname" + i.ToString();
           emp.LastName = "Lname" + i.ToString();
           emp.EmployeeCard = new EmployeeCard();
           emp.EmployeeCard.StartWorkingDate = DateTime.Now.Date.AddDays(-i);
           empList.Add(emp);
            for(int j=0;j<2;j++)
            {
                Children child = new Children();
                child.FirstName = "ChildFname" + j.ToString();
                child.LastName = "ChildLname" + j.ToString();
                empList.Children.Add(child);
            }
       }
       return empList;
   }
}

public class Child
{
   public virtual string FirstName { get; set; }
   public virtual string LastName { get; set; }
   public virtual Employee Employee { get; set; }  
}

public class EmployeeCard
{
   public virtual Employee Employee { get; set; }
   public virtual DateTime? StartWorkingDate { get; set; }
}

当我在 运行 将员工列表绑定到报告时,出现此错误

cannot bind to the provided datasource because it's not supported or not implemented in our supported interfaces.

当我从员工那里删除参考员工卡时 class 它工作得很好。如何使用员工列表将employeecard的详细信息绑定到报表??

这里是how I created the xtraReports

的示例代码

更新 1

这就是我构建员工对象并将它们绑定到报表的方式

XtraReport report = new XtraReport();
        List<Employee> ReportDataSource = Employee.GetData();
        ReportHeaderBand headerBand = new ReportHeaderBand() {
            HeightF = 80
        };
        report.Bands.Add(headerBand);

        headerBand.Controls.Add(new XRLabel() {
            Text = "Employee Report",
            SizeF = new SizeF(650, 80),
            TextAlignment = TextAlignment.BottomCenter,
            Font = new Font("Arial", 36)
        });


        DetailBand detailBandEmployee = new DetailBand();
        var detailReportBandEmployee = new DetailReportBand
        {
            KeepTogether = true,
            DataMember ="",
            DataSource = ReportDataSource
        };
        detailReportBandEmployee.Bands.Add(detailBandEmployee);



        XRLabel lbFname = new XRLabel() {
            LocationF = new PointF(200, 10),
            SizeF = new SizeF(440, 50),
            TextAlignment = TextAlignment.BottomLeft,
            Font = new Font("Arial", 24)
        };
        detailBandEmployee.Controls.Add(lbFname);
        lbFname.DataBindings.Add("Text", null, "FirstName");

        XRLabel lbLastName = new XRLabel() {
            LocationF = new PointF(200, 60),
            SizeF = new SizeF(440, 40),
            TextAlignment = TextAlignment.TopLeft,
            Font = new Font("Arial", 14, FontStyle.Italic)
        };
        detailBandEmployee.Controls.Add(lbLastName);
        lbLastName.DataBindings.Add("Text", null, "LastName");
        DetailBand detailBandEmployeeChild = new DetailBand();
        var detailReportBandEmployeeChild = new DetailReportBand
        {
            KeepTogether = true,
            DataMember = "Children",
            DataSource = ReportDataSource
        };
        detailReportBandEmployeeChild.Bands.Add(detailBandEmployeeChild);



        XRLabel lbChildFname = new XRLabel()
        {
            LocationF = new PointF(200, 10),
            SizeF = new SizeF(440, 50),
            TextAlignment = TextAlignment.BottomLeft,
            Font = new Font("Arial", 24)
        };
        detailBandEmployeeChild.Controls.Add(lbChildFname);
        lbChildFname.DataBindings.Add("Text", null, "FirstName");

        XRLabel lbChildLastName = new XRLabel()
        {
            LocationF = new PointF(200, 60),
            SizeF = new SizeF(440, 40),
            TextAlignment = TextAlignment.TopLeft,
            Font = new Font("Arial", 14, FontStyle.Italic)
        };
        detailBandEmployeeChild.Controls.Add(lbChildLastName);
        lbChildLastName.DataBindings.Add("Text", null, "LastName");
        DetailBand detailBandEmployeeCard = new DetailBand();
        var detailReportBandEmployeeCard = new DetailReportBand
        {
            KeepTogether = true,
            DataMember = "EmployeeCard  ",
            DataSource = ReportDataSource
        };
        detailReportBandEmployeeCard.Bands.Add(detailBandEmployeeCard);



        XRLabel lbStartDate = new XRLabel()
        {
            LocationF = new PointF(200, 10),
            SizeF = new SizeF(440, 50),
            TextAlignment = TextAlignment.BottomLeft,
            Font = new Font("Arial", 24)
        };
        detailBandEmployeeCard.Controls.Add(lbStartDate);
        lbStartDate.DataBindings.Add("Text", null, "StartWorkingDate");

        detailReportBandEmployee.Bands.Add(detailReportBandEmployeeCard);
        detailReportBandEmployee.Bands.Add(detailReportBandEmployeeChild);
        report.Bands.Add(detailReportBandEmployee);

问题是您是这样为卡片创建详细信息带的

var detailReportBandEmployeeCard = new DetailReportBand
{
     KeepTogether = true,
     DataMember = "EmployeeCard",
     DataSource = ReportDataSource
};

并将数据源绑定到此 属性

public virtual EmployeeCard EmployeeCard { get; set; }

您只能使用列表作为数据源,EmployeCard 不是列表。 如果您只有一个对象,则不需要 detailBandEmployeeCard,您可以将标签直接放入 detailBandEmployee

XRLabel lbStartDate = new XRLabel()
{
    LocationF = new PointF(200, 10),
    SizeF = new SizeF(440, 50),
    TextAlignment = TextAlignment.BottomLeft,
    Font = new Font("Arial", 24)
};
//detailBandEmployeeCard.Controls.Add(lbStartDate);
//lbStartDate.DataBindings.Add("Text", null, "StartWorkingDate");

detailBandEmployee.Controls.Add(lbLastName);
lbStartDate.DataBindings.Add("Text", null, "EmployeeCard.StartWorkingDate");

GetData() 在应该是 Child 时使用 class Children,它也不会填充列表。我想循环一定是这样的

for (int j = 0; j < 2; j++)
{
     Child child = new Child();
     child.FirstName = "ChildFname" + j.ToString();
     child.LastName = "ChildLname" + j.ToString();
     emp.AddChild(child);
}