使用DataGridView作为报表查看器的数据源

时间:2016-10-03 18:16:25

标签: c# winforms 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;
}

但我有一份空的报告。

1 个答案:

答案 0 :(得分: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();
        }