【问题标题】:how to bind datasource to a .rdlc report in c#如何在 C# 中将数据源绑定到 .rdlc 报告
【发布时间】:2013-03-31 10:32:09
【问题描述】:

朋友们,我用c#开发了一个简单的应用程序,它有两个rdlc报告

我使用下面的代码将数据源绑定到报告查看器

 this.reportViewer1.LocalReport.ReportPath = @"C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\reports\reports\Report1.rdlc";
 reportViewer1.LocalReport.DataSources.Clear();
 reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("customer", dt.Tables[0])) ;
 this.reportViewer1.RefreshReport();

但是当生成报告时,它是空报告,不会显示任何数据,有什么意见???

【问题讨论】:

  • dt.Tables[0] 真的包含数据吗?另外:您报告中的数据源是否称为customer
  • @marc_s - 是的,我用 forloop 测试了数据在客户表的数据集中,reportDataSource 名称应该是什么?表名还是数据集名? `reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("customer", dt.Tables[0])) ;`
  • 数据源的名称在您的报告中需要与您在调用new ReportDataSource(....) 时使用的名称相同 - 如果您在调用它时调用它customer创建ReportDataSource.RDLC报表中的数据源名称也必须是Customer
  • @marc_s - 抱歉问这个问题,如何将datasource 分配给.RDLC 报告?

标签: c# winforms dataset rdlc


【解决方案1】:

当您通过向导在项目中添加 .rdlc 报告时,默认情况下它将数据集名称设为 'DataSet1' 。现在,如果您想动态绑定新数据集,那么该数据集的名称必须是 'DataSet1'。尝试更改它并检查 Table[0] 是否包含一些 DataTypeDataSet1 的原始数据类型匹配的数据(行)。如果 DataType 不匹配,则数据不会出现在您的 ReportViewer 中。试试这个代码:-

string exeFolder = (Path.GetDirectoryName(Application.StartupPath)).Substring(0, (Path.GetDirectoryName(Application.StartupPath)).Length - 3);
string reportPath = Path.Combine(exeFolder, @"Reports\SessionReport.rdlc");
Microsoft.Reporting.WinForms.ReportDataSource rds = new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1", yourDataSet.Tables[0]);
this.reportViewer1.LocalReport.DataSources.Add(rds);
this.reportViewer1.LocalReport.ReportPath = reportPath;
this.reportViewer1.RefreshReport();

有关 .rdlc 报告(核心逻辑)的更多详细信息,请参阅以下链接 How to create report (RDLC) without database?

【讨论】:

    【解决方案2】:

    试试下面,可能是数据源名称不正确的问题。

    reportViewer1.LocalReport.DataSources.Add(new ReportDataSource(ds.DataSetName + "_" + ds.Tables[0].TableName, ds.Tables[0]));
    

    您可以在 rdlc 文件内容中查看数据集名称。检查数据集的名称属性是否与您在代码中给出的匹配。

    【讨论】:

      【解决方案3】:

      这是我使用对象绑定更新数据的方式: 在 Form1.cs 文件中:

      private myClass m_products = new Products();
      public Form1()
              {
                  InitializeComponent();
              }
      
              private void Form1_Load(object sender, EventArgs e)
              {
                 this.PaperBindingSource.DataSource = m_products.GetProducts();
      

      this.PaperBindingSource.DataSource 很重要。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-02
        • 2019-03-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多