【问题标题】:Use a DataGridView as data source to Report Viewer使用 DataGridView 作为报表查看器的数据源
【发布时间】:2016-10-05 02:07:27
【问题描述】:

我正在尝试使用 DataGridView 作为我的报告的数据源,我想在我的报告中显示 DataGridView 中的表格。我搜索并在 SO 中找到的代码告诉我做这样的事情:

生成报告的按钮

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;
}

但我有一个空的报告。

【问题讨论】:

  • 您是否收到异常或报告查看器中显示任何错误?
  • 您不应该向我们展示您的 ReportForm 的构造函数吗?将构造函数的参数更改为只传递 DataTable,而不是整个网格控件。
  • 不只是一个空表单,我将使用 ReportForm 的构造函数更新问题,但我已经尝试只传递 dataTable 并且得到相同的结果。
  • 问题无法通过这种方式重现。我可以猜测某些情况,例如您可能有一个空报告,您可能有一个空数据表,您可能错误地使用了另一个报告。在我目前想到的其他情况下,您应该收到一个例外。因此,请确保您的报告完全显示数据。另请查看this post 并按照步骤获得一个工作示例。这是一个循序渐进的例子。
  • 加载事件是否连接好?

标签: c# winforms datagridview


【解决方案1】:

我解决了在我的应用程序中添加 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();
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-23
    • 2011-09-22
    • 1970-01-01
    • 1970-01-01
    • 2011-09-30
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    相关资源
    最近更新 更多