2015-10-28 44 views
5

我有一個reportViewer和多個報告(例如Report1.rdlc,Report2.rdlc,ecc), 我怎麼能以編程方式在它們之間切換?以編程方式將報告分配給我的reportViewer

我能夠分配不同的報告,但是當我執行程序時,它說我需要分配數據來源,我該如何做到這一點?

編輯:這是到目前爲止我的代碼:

public Report() 
{ 
    InitializeComponent(); 

    this.View_StatoMagTableAdapter.Fill(this.NGStoreV2DataSet.View_StatoMag); 
    this.mag2TableAdapter.Fill(this.NGStoreV2DataSet.mag2); 

    this.mag2BindingSource.DataMember = "mag2"; 
    this.mag2BindingSource.DataSource = this.NGStoreV2DataSet; 
} 

private void reportViewer1_Load(object sender, EventArgs e) 
{ 
    this.reportViewer1.Reset(); 

    var binding = new BindingSource(); 
    binding.DataSource = this.NGStoreV2DataSet.mag2; 

    ReportDataSource rds = new ReportDataSource("NGStoreV2DataSet", binding); 
    this.reportViewer1.LocalReport.DataSources.Clear(); 
    this.reportViewer1.LocalReport.DataSources.Add(rds); 
    this.reportViewer1.LocalReport.ReportEmbeddedResource = "ReportViewerForm.Report2.rdlc"; 
    this.reportViewer1.RefreshReport(); 
} 

新版本仍然無法正常工作,當我運行它,它仍然要求提供數據來源。

我已經嘗試了不同的組合,但沒有一個可以工作。 組合,如:

var binding = new BindingSource(); 
binding.DataSource = this.NGStoreV2DataSet.mag2; 

ReportDataSource rds = new ReportDataSource("NGStoreV2DataSet", binding); 

ReportDataSource rds = new ReportDataSourc("NGStoreV2DataSet", this.mag2BindingSource); 

編輯:我終於設法解決這個問題!我使用了錯誤的數據集(而不是報表數據集至極NGStoreV2DataSet是數據集1) 謝謝既tezzo和哈迪的大力幫助;)

+0

你是如何設置它的?你的實施在哪裏? – felipekm

+0

編輯的問題 – Spartan

回答

5

您需要同時設置ReportPathDataSources

YourReportViewer.LocalReport.ReportEmbeddedResource = "ReportViewerForm.Report1.rdlc" 
YourReportViewer.LocalReport.DataSources.Clear() 
YourReportViewer.LocalReport.DataSources.Add(New ReportDataSource("YourTableName", yourDataTable)) 
+0

這也可以幫助:http://stackoverflow.com/questions/22017968/how-to-bind-dynamically-datasource-to-reportviewer-on-windows-forms-c-sharp – felipekm

+0

謝謝,但我錯過了變量'yourDataTable'的來源。 – Spartan

+0

'yourDataTable'是一個充滿數據的數據表,用於顯示報表;請參閱MSDN上的此鏈接:https://msdn.microsoft.com/en-us/library/ms171920.aspx – tezzo

2

你可以做以下

var binding = new BindingSource(); 
binding.DataSource = yourData; 
reportViewer1.Reset(); 
reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("NGStoreV2DataSet", 
        binding)); 
reportViewer1.LocalReport.ReportEmbeddedResource = "ReportViewerForm.Report1.rdlc"; 
reportViewer1.RefreshReport(); 

希望這將幫助你

+0

它不起作用,它仍然要求數據來源,我編輯我的問題與實際的代碼 – Spartan

0

/* 使一個DataTable這是價值,清算地方是重要的,你叫什麼都你的數據集在報表更好地匹配的名稱 */

的DataTable DGraph組件= clsDailyReports.MakeTmpDataSet.Invoke(CON,SqlAtd).Tables [0];

rpt.LocalReport.DataSources.Clear(); 

    Microsoft.Reporting.WebForms.ReportDataSource rptdBody = new Microsoft.Reporting.WebForms.ReportDataSource(); 
    rptdBody.Name = "DataSet1"; 
    rptdBody.Value = dBody; 
    rpt.LocalReport.DataSources.Add(rptdBody); 

    Microsoft.Reporting.WebForms.ReportDataSource rptdTop = new Microsoft.Reporting.WebForms.ReportDataSource(); 
    rptdTop.Name = "DataSet2"; 
    rptdTop.Value = dGraph; 
    rpt.LocalReport.DataSources.Add(rptdTop); 

    DataTable dDate = clsDailyReports.MakeTmpDataSet.Invoke(con, sSqlDate).Tables[0]; 
    Microsoft.Reporting.WebForms.ReportDataSource rptDate = new Microsoft.Reporting.WebForms.ReportDataSource(); 
    rptDate.Name = "DataSet3"; 
    rptDate.Value = dDate; 
    rpt.LocalReport.DataSources.Add(rptDate); 

    rpt.LocalReport.ReportPath = System.Web.HttpContext.Current.Server.MapPath(@"~\Reports\rptUnAdjustedPeriodTotals.rdlc"); 
    rpt.LocalReport.Refresh(); 
相關問題