使用DataGridView作为报表查看器的数据源
本文关键字:数据源 报表 DataGridView 使用 | 更新日期: 2023-09-27 18:11:23
我试图使用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;
}
但是我有一个空的报告。
我修复了在应用程序中添加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();
}