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

时间:2015-08-14 22:17:03

标签: c# linq devexpress xtrareport

我有三个类(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; }
}

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

  

无法绑定到提供的数据源,因为它不受支持或   未在我们支持的界面中实现。

当我从员工类中删除参考员工卡时它完美无缺。如何使用员工列表将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);

1 个答案:

答案 0 :(得分:2)

问题是您为此卡创建了详细信息区域

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,它应该是Child,也不要填充列表。我认为循环必须是这样的

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