2016-10-03 86 views
0

我試圖將DataGridView作爲數據源用於報表,我想在報表中顯示DataGridView中的表。我搜索,我發現這裏的代碼,以便告訴我做這樣的事情:在我的報表生成報告使用DataGridView作爲報表查看器的數據源

private void GenerateReport_Click(object sender, EventArgs e) 
{ 
    ReportForm reportForm = new ReportForm(DataGrid); 
    reportForm.ShowDialog(); 
} 

按鈕:

private DataGridView grid; 

private void ReportForm_Load(object sender, EventArgs e) 
{ 
    DataTable dt = (DataTable)grid.DataSource; 
    dt.TableName = "reportSource"; 

    reportViewer1.ProcessingMode = ProcessingMode.Local; 
    ReportDataSource rds = new ReportDataSource("reportSource", dt); 
    reportViewer1.LocalReport.DataSources.Clear(); 
    reportViewer1.LocalReport.DataSources.Add(rds); 
    this.reportViewer1.RefreshReport(); 
    } 

ReportForm的構造

public ReportForm(DataGridView grid) 
{ 
    InitializeComponent(); 
    this.grid = grid; 
} 

但我有一個空的報告。

+0

你收到一個異常或報表查看器顯示任何錯誤? –

+1

您不應該向我們展示您的ReportForm的構造函數嗎?將您的構造函數的參數更改爲傳遞DataTable,而不是整個網格控件。 – LarsTech

+0

不只是一個空的表單,我將使用ReportForm的構造函數更新問題,但我已經嘗試傳遞dataTable,並得到相同的結果。 – user3670112

回答

0

我解決我的問題在我的應用程序添加一個DataSet對象,然後我用這個代碼:

 private void ReportForm_Load(object sender, EventArgs e) 
     { 
      DataTable dt = (DataTable)grid.DataSource; 
      ProgrammersDataSet ds = new ProgrammersDataSet(); 
      ds.Tables.Add(dt.Copy()); 
      ds.Tables[1].TableName = "ProgrammersDataSet"; 
      ReportDataSource rds = new ReportDataSource(ds.Tables[1].TableName, ds.Tables[1]); 

      reportViewer1.ProcessingMode = ProcessingMode.Local; 
      reportViewer1.LocalReport.DataSources.Clear(); 
      reportViewer1.LocalReport.DataSources.Add(rds); 
      reportViewer1.LocalReport.Refresh(); 
      reportViewer1.LocalReport.ReportEmbeddedResource = "Cerocha.Presentation.Reports.ProgrammersReport.rdlc"; 
      this.reportViewer1.RefreshReport(); 
     } 
相關問題