如何在报表查看器中使用DataTable的数据?

时间:2014-04-06 11:33:34

标签: c# winforms reportviewer

我将datagridview的数据源作为datatable并将其传递给reportviewer

        DataTable dd = (DataTable)DGVCars.DataSource;
        dd.TableName = "Cars";
        Report r = new Report(dd);
        r.Show();

       // MaterialsSuppliersDataSet t = new MaterialsSuppliersDataSet();

       // MessageBox.Show("" + dd.Rows[0][1]);

        ReportDataSource RDS = new ReportDataSource("Cars",dd);


        RV.ProcessingMode = ProcessingMode.Local;
        LocalReport lc = RV.LocalReport;
        lc.DataSources.Add(RDS);

        RV.LocalReport.ReportPath = "Report1.rdlc";

        this.RV.RefreshReport();

如何使用数据表的字段在报告中显示?

2 个答案:

答案 0 :(得分:0)

要将记录绑定到报告查看器,请使用Databind。

this.ReportViewer1.LocalReport.ReportPath = Server.MapPath("Report1.rdlc");
ReportDataSource rds = new ReportDataSource("Cars", Dataset);
this.ReportViewer1.ProcessingMode = ProcessingMode.Local
this.ReportViewer1.LocalReport.DataSources.Clear();
this.ReportViewer1.LocalReport.DataSources.Add(rds);
this.ReportViewer1.DataBind();
this.ReportViewer1.LocalReport.Refresh();

请详细参阅this codeproject文章。

答案 1 :(得分:0)

请按照以下步骤操作:

1)在项目中添加新的类库项目。

2)将数据表的列定义为此类的属性,如下图所示:

   String _name;
   Public String name
   {
       get {return _name;}
       set {_name=Value;}
   }

3)建立你的项目。

4)将报告向导项添加到项目中。

5)为新的rdlc报告定义一个具有对象类型的数据源,并选择您的类(在步骤1中定义)作为数据集。

现在,您可以在报告中看到类的属性(与数据表的列相同)。

相关问题