2009-01-23 60 views
2

我試圖讓嵌套的對象在Microsoft報告中工作。我從http://www.gotreportviewer.com/objectdatasources/index.html下載了示例代碼,並且它運行正常。微軟報告嵌套對象數據源給出#Error

我建立了一個基於Windows窗體和他們的代碼,以及所有在下面的小應用程序時,我引用一個嵌套的對象值是「#錯誤」在數據應該出現的地方,我曾經得到。

在報告中,我使用的建議在網站的同一嵌套對象語法:

=Fields!Name.Value.FirstName 

它適用於他們在我的電腦上的應用程序,而不是我的。我無法理解它!有沒有人遇到過這種情況,或知道爲什麼會發生這種情況

而且 - 我不知道這是有關 - 我不能給LocalReport.DataSources對象添加ClientItem的單個實例。它必須是一個列表。但是,在呈現時,它只會在報表的表格中顯示一行(#Errored)數據。

任何幫助,將不勝感激!

namespace ReportTest 
{ 

    public class ClientItem 
    { 
     public int Id { get; set; } 
     public ClientName Name { get; set; } 
    } 

    public class ClientName 
    { 
     public ClientName(string first, string last) 
     { 
      FirstName = first; 
      LastName = last; 
     } 

     string FirstName { get; set; } 
     string LastName { get; set; } 
    } 

    public partial class Form1 : Form 
    { 
     private List<ClientItem> clients = new List<ClientItem>(); 

     public Form1() 
     { 
      InitializeComponent(); 
      PopulateLists(); 
      GenerateReport(); 
     } 

     private void PopulateLists() 
     { 
      clients.Add(new ClientItem { Id = 1, Name = new ClientName("Adrian", "Adesco") }); 
      clients.Add(new ClientItem { Id = 2, Name = new ClientName("Brian", "Briar") }); 
      clients.Add(new ClientItem { Id = 3, Name = new ClientName("Clive", "Cussler") }); 
     } 

     private void GenerateReport() 
     { 
      this.Text = "Report Control Demo"; 
      this.ClientSize = new System.Drawing.Size(950, 600); 

      ReportViewer reportViewer = new ReportViewer(); 

      reportViewer.ProcessingMode = ProcessingMode.Local; 

      reportViewer.LocalReport.ReportPath = "TestReport.rdlc"; 

      reportViewer.LocalReport.DataSources.Add(new ReportDataSource("ReportTest_ClientItem", clients)); 

      reportViewer.Dock = DockStyle.Fill; 
      this.Controls.Add(reportViewer); 

      reportViewer.RefreshReport(); 
     } 
    } 
} 

回答

1

好,與測試用例(以上)的問題的解決方案是使CLIENTNAME公共屬性:

public class ClientName 
{ 
    public ClientName(string first, string last) 
    { 
     FirstName = first; 
     LastName = last; 
    } 

    public string FirstName { get; set; } 
    public string LastName { get; set; } 
} 

這解決了測試用例的問題對我來說。

然而,我仍然有我的實際報告的問題。這仍然是一個錯誤。事實證明,這是因爲子對象實際上是在不同的程序集中定義的。

爲了它的工作,下面的行必須添加到包含子對象的項目的AssemblyInfo.cs文件:

[assembly: AllowPartiallyTrustedCallers] 

現在,它的作品!過了好半天才發現 - 希望這可以幫助別人......

+0

我有一個非常類似的問題給你,但加入'AllowPartiallyTrustedCallers`並沒有爲我工作。你對這個問題有什麼其他建議嗎? – meanbunny 2013-01-31 18:33:42