【问题标题】:how to set Datatable as datasource in ReportViewer如何在 ReportViewer 中将 Datatable 设置为数据源
【发布时间】:2016-03-31 05:33:41
【问题描述】:

我在ReportViewer 中搜索关于Datatable 作为datasource 的最后一个问题,我发现这是解决方案

DataTable table = new DataTable();
table.Columns.Add("value", typeof(string));
table.Columns.Add("price", typeof(string));
table.Columns.Add("quantity", typeof(string));

table.Rows.Add("test1","10","20");
table.Rows.Add("test2", "10", "20");

reportViewer1.LocalReport.DataSources.Clear();

ReportDataSource rprtDTSource = new ReportDataSource("TITLE",table);

reportViewer1.LocalReport.DataSources.Add(rprtDTSource);
reportViewer1.RefreshReport();

但我得到这张图片作为结果

有什么问题??

【问题讨论】:

  • 您在哪里设置报表查看器的报表?我想您有一个报告作为嵌入式资源,然后您可以将报告源设置为:this.reportViewer1.LocalReport.ReportEmbeddedResource = "Namespace.ReportName.rdlc";
  • 我只有数据表和报表查看器,如何在报表查看器@RezaAghaei 中显示数据

标签: c# winforms datatable reportviewer localreport


【解决方案1】:

您似乎忘记为您的报表查看器控件设置报表源。您可以使用以下任一选项设置报告来源:

例如,我假设你已经在你的项目中添加了一个报表,那么你可以这样在报表查看器中显示它:

var reportDataSource1 = new ReportDataSource("NameOfReportDataSet", YourDataTable);
this.reportViewer1.LocalReport.DataSources.Add(reportDataSource1);
this.reportViewer1.LocalReport.ReportEmbeddedResource = "Namespace.ReportName.rdlc";
this.reportViewer1.RefreshReport();

您也可以使用设计器简单地设置报表查看器的报表。在您的表单上放置一个报表查看器,然后单击右上角的箭头打开报表查看器的智能标记窗口,然后从组合框中选择一个报表。

【讨论】:

  • 我只有数据表和报表查看器,我如何在报表查看器中显示数据,你能举个例子吗?请!
  • 是的,我做了这个操作,将一个新的空报表添加到项目并选择它作为报表查看器的资源
  • 创建一个空的报告是没有用的。您应该在报表中添加数据源并设计报表。然后将您的数据传递给报告。您可以使用报告向导。
  • 干得好,您是如何在报告中显示数据库的数据的。您可以以相同的方式显示自定义数据表的数据。另外您应该注意,您的自定义数据表架构应该与您用于创建报告的表一样。
  • 另外你应该知道如果你有例如ProductDataTable你不需要创建一个自定义表格并将它传递给你的报告,你可以使用那个表格。特别是如果您创建了一个包含数据表的数据集,并且您正在使用绑定源作为报表的数据源,那么您可以简单地在yourSataSet.YourTable.Rows 的实例中添加一些行,然后RefreshReport
【解决方案2】:

如果我没记错的话,您使用的 ReportDataSource ctor 需要第一个参数中的数据源,即命名数据源。你不提供这个,你需要 DataTable 名称。

将您的代码更新为:

DataTable dt = new DataTable();
dt.TableName = "myDataTable";
//Fill Datatable
ReportDataSource source = new ReportDataSource("myDataTable", dt);

【讨论】:

    【解决方案3】:

    你可以像下面这样添加源

    LocalReport report = new LocalReport();
    
    string startupPath = Environment.CurrentDirectory;
    report.ReportPath = startupPath + @"\RDCLTemplate.rdlc";
    report.Refresh();
    

    【讨论】:

      猜你喜欢
      • 2016-02-16
      • 1970-01-01
      • 1970-01-01
      • 2017-10-26
      • 2011-08-03
      • 2013-07-01
      • 2013-02-10
      • 2012-07-28
      • 2013-03-26
      相关资源
      最近更新 更多