C# 将DataGridView用作报表查看器的数据源

C# 将DataGridView用作报表查看器的数据源,c#,winforms,datagridview,C#,Winforms,Datagridview,我试图使用DataGridView作为报表的数据源,我想在报表中的DataGridView中显示该表。我在这里搜索了我在中找到的代码,请告诉我这样做: 生成报告的按钮 private void GenerateReport_Click(object sender, EventArgs e) { ReportForm reportForm = new ReportForm(DataGrid); reportForm.ShowDialog(); } 在我的报告中: private

我试图使用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();
  }
报表窗体的构造函数

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

但是我有一个空报告。

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

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