2015-08-14 99 views
1

我有三個類(員工,EmployeeCard和兒童)實施這樣DevExpress的動態創建ExtraReports和動態綁定到對象作爲數據源

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

,當我在運行時綁定的員工名單一個報告我得到這個錯誤

無法綁定到提供的數據源,因爲它不被支持或 未在我們支持的接口中實現。

當我從僱員類中刪除參考僱員卡它完美的工作。我如何使用僱員名單將僱員卡的細節綁定到報表上?

這裏是how I created the xtraReports

更新1

的示例代碼,這是我如何建立Employee對象,並將其綁定到報告

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

你可以發佈你如何做綁定以及如何構建員工對象? – deramko

+0

@deramko查看更新 –

回答

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()使用類兒童時,它應是輕而易舉的,它也沒有填充列表。我認爲循環必須是這樣的

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