【问题标题】:How to bind a report to a DataSource and send it to the ReportViewer如何将报表绑定到 DataSource 并将其发送到 ReportViewer
【发布时间】:2017-02-22 23:16:42
【问题描述】:

我正在尝试使用 Telerik 报告框架构建报告系统。到目前为止,我已经能够使用 vs 设计器创建报告,但还没有设法将附件附加到任何类型的动态数据中。

我在 ASP.NET MVC 项目中执行此操作(不是 Telerik MVC 项目。这有关系吗?)。

我目前有:

  1. 使用报表查看器向导时自动创建的控制器
  2. 报告
  3. 我想用来填充报告的模型

这是我认为的代码:

@model Telerik.Reporting.ReportSource

@(Html.TelerikReporting().ReportViewer()
        .Id("reportViewer1")
        .ServiceUrl(Url.Content("~/api/reports"))
        .ReportSource(model)
        .ViewMode(ViewMode.Interactive)
        .ScaleMode(ScaleMode.Specific)
        .Scale(1.0)
        .PersistSession(false)
        .PrintMode(PrintMode.AutoSelect)
)

让我立即感到困惑的一件事是如何定义要获取的数据?

假设我有一个模型要用于填充报告:

public class ReportModel()
{
    Dictionary<string, int> Graphdata;

    public ReportModel()
    {
         Graphdata = GetGraphData();
    }

    public Dictionary<string, int> GetGraphData()
    {
    ...
    }
}

所以我会在 Reports 控制器中创建一个新报告:

var report = new Report1();

现在我必须为报告创建数据源。这就是我被困的地方......

报告的模型将包含报告中所有不同元素的多个列表、表格、字典。

在文档中,他们这样做:

// Creating and configuring the ObjectDataSource component:
var objectDataSource = new Telerik.Reporting.ObjectDataSource();
objectDataSource.DataSource = typeof(Products); // Specifying the business object type
objectDataSource.DataMember = "GetProducts"; // Specifying the name of the data object method
objectDataSource.CalculatedFields.Add(new Telerik.Reporting.CalculatedField("FullName", typeof(string), "=Fields.Name + ' ' + Fields.ProductNumber")); // Adding a sample calculated field.

// Specify the parameters, their types and values
objectDataSource.Parameters.Add(new Telerik.Reporting.ObjectDataSourceParameter("color", typeof(string), "Silver"));
objectDataSource.Parameters.Add(new Telerik.Reporting.ObjectDataSourceParameter("productModelID", typeof(int), 23));

这就是我迷路的地方。所以这是我的问题:

如果报表上有多个元素,是否必须为每个元素创建一个数据源?

将数据绑定到报表的正确 MVC 方法是什么?

如果reportviewer 是通过API 获取实际的报告,那么API 如何知道将创建报告对象时绑定的数据发送给它?

【问题讨论】:

    标签: asp.net asp.net-mvc telerik telerik-reporting


    【解决方案1】:

    我在 ASP.NET MVC 项目中执行此操作(不是 Telerik MVC 项目。 有关系吗?)。

    不,应该没有区别。 Telerik MVC 项目只是为您完成了一些初始设置和 dll 引用。

    让我立即感到困惑的一件事是如何定义哪些数据 要取吗?

    最好的方法是在设计时定义数据并使用数据浏览器或数据源向导绑定它,或者在运行时使用报表的 NeedDataSource 事件通过其他方式构建数据。

    参考资料:

    如果我在报告中有多个元素,我是否必须创建一个 他们每个人的数据源?

    这取决于很多事情......“元素”是什么意思? - 大多数事情都不需要单独的数据源。使用单独的数据源可能会更好地设计一些子报表,但如果您能够将数据源从父报表传递到子报表,那么大多数情况下也没有必要这样做。

    参考:http://docs.telerik.com/reporting/data-items-binding-a-data-item-to-data#binding-to-data-from-the-parent-data-item

    将数据绑定到报表的正确 MVC 方法是什么?

    在设计时使用数据资源管理器绑定数据,或者使用 Rerport 的 NeedDataSource 在运行时生成数据,如上所述。任一选项都可以支持参数,以允许您根据来自报表查看器的输入变量为报表构建“动态”数据。

    如果reportviewer 是通过API 获取实际报告,那么该api 如何知道将创建报告对象时绑定的数据发送给它?

    报表查看器负责加载报表。报告应负责获取和绑定自己的数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多