2013-04-09 26 views
2

友.rdlc報告,我已經開發使用C#一個簡單的應用程序,它有兩個RDLC報告如何綁定數據源在C#

我用下面這段代碼綁定的數據源的報表查看器

this.reportViewer1.LocalReport.ReportPath = @"C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\reports\reports\Report1.rdlc"; 
reportViewer1.LocalReport.DataSources.Clear(); 
reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("customer", dt.Tables[0])) ; 
this.reportViewer1.RefreshReport(); 

但是當報告生成時,它是空的報告沒有數據會顯示,任何意見???

+0

ASP.net或WinForm的? – Damith 2013-04-09 06:10:22

+0

@ Damith - Winforms – Roshan 2013-04-09 06:12:51

+0

'dt.Tables [0]'是否真的包含數據?另外:您的報告中的數據源是「客戶」嗎? – 2013-04-09 06:13:52

回答

2

請在下面嘗試,可能是數據源名稱不正確的問題。

reportViewer1.LocalReport.DataSources.Add(new ReportDataSource(ds.DataSetName + "_" + ds.Tables[0].TableName, ds.Tables[0])); 

您可以檢查rdlc文件內容的數據集名稱。檢查數據集名稱屬性與您在代碼中給出的內容匹配。

0

這是我如何與對象綁定更新我的數據: 在Form1.cs文件:

private myClass m_products = new Products(); 
public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      this.PaperBindingSource.DataSource = m_products.GetProducts(); 

this.PaperBindingSource.DataSource是很重要的。

7

當您通過嚮導在項目中添加.rdlc報告時,默認情況下它將數據集名稱設置爲'DataSet1'。現在,如果要動態綁定新數據集,那麼該數據集的名稱必須爲'DataSet1'。嘗試更改並檢查表[0]是否包含數據類型與原始數據類型DataSet1匹配的一些數據(行)。如果DataType不匹配,那麼數據將不會進入您的ReportViewer。試試這個代碼: -

string exeFolder = (Path.GetDirectoryName(Application.StartupPath)).Substring(0, (Path.GetDirectoryName(Application.StartupPath)).Length - 3); 
string reportPath = Path.Combine(exeFolder, @"Reports\SessionReport.rdlc"); 
Microsoft.Reporting.WinForms.ReportDataSource rds = new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1", yourDataSet.Tables[0]); 
this.reportViewer1.LocalReport.DataSources.Add(rds); 
this.reportViewer1.LocalReport.ReportPath = reportPath; 
this.reportViewer1.RefreshReport(); 

有關.rdlc報告(核心邏輯)詳細請參考以下鏈接 How to create report (RDLC) without database?