VB.Net:如何在报表 (.rdlc) 中使用对象数据源 [英] VB.Net: How to use an Object data source in report (.rdlc)

查看:39
本文介绍了VB.Net:如何在报表 (.rdlc) 中使用对象数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题类似于这个 但我在实际实施中遇到了一些问题.

My question is similar to this one but I'm having some problems with the actual implementation.

我在 3 层应用的业务层有一份报告 (.rdlc).

I've got a report (.rdlc) in the business layer of a 3-tier app.

我在 BL (EmployeeManager) 中有一个对象,它有一个 GetEmployees(Expression as Expression(Of Func(Of Employee, Boolean))) As IQueryable(Of Employee) 方法.

I've got an object in the BL (EmployeeManager) which has a GetEmployees(Expression as Expression(Of Func(Of Employee, Boolean))) As IQueryable(Of Employee) method.

因为我不想尝试直接传递 lambda(至少在我有一些工作之前不会),我在 BL 中创建了一个 ReportData 类,它包装了GetEmployees() 调用并将结果公开为 IEnumerable(Of Employee) - 这应该非常简单.目前它甚至没有参数.

As I didn't want to try and pass a lambda in directly (at least not until I've got something working), I've created a ReportData class in the BL which wraps the GetEmployees() call and exposes the results as an IEnumerable(Of Employee) - which should be very simple. It doesn't even have parameters at the moment.

好的... 所以在我的报告中,我尝试添加一个新的数据源.我选择了一种 Object 并找到了上面提到的 ReportData 类.向导完成并将 DataSources 文件夹添加到项目中,其中包含一些定义 并指向 Report 类的 XML.

Ok... So In my report, I've tried to add a new Data Source. I've picked a type of Object and located the ReportData class mentioned above. The wizard completes and adds a DataSources folder to the project inside which is some XML defining a <GenericObjectDataSource> and pointing at the Report class.

ReportData 也出现在数据源"窗格中 - 它旁边有一个 > 但当我展开它时,它没有子项.

ReportData also appears in the Data Sources pane - It has a > next to it but when I expand it, it has no children.

我不知道怎么做的是使用数据源 - 它似乎没有公开任何方法/成员(我什至没有指定它应该调用 GetEmployees()!)而且我当然在任何地方都看不到 IEnumerable(Of Employee).

What I don't know how to do is USE the data source - It doesn't seem to expose any methods/members (I haven't even specified that it should call GetEmployees() yet!) and I certainly can't see an IEnumerable(Of Employee) anywhere.

当我尝试向报表添加表格时,它提示我选择一个数据集,ReportData 数据源未显示在数据源下拉列表中.

When I try to add a table to the report and it prompts me to select a Dataset, the ReportData Datasource is not shown in the Data source drop-down.

我错过了什么?有人可以指出我正确的方向吗?

What am I missing? Can someone please point me in the right direction?

我的简单 ReportData 对象:

My simple ReportData object:

Namespace Reports
    Public Class ReportData

        Private Property EmployeeManager As Interfaces.IEmployeeManager

        Public Sub New()
            ''This sub is here in case it's an instantiation problem - I intend to use dependency injection when I've got this working properly.
            Me.EmployeeManager = New EmployeeManager
        End Sub

        Public Sub New(ByVal EmployeeManager As Interfaces.IEmployeeManager)
            Me.EmployeeManager = EmployeeManager
        End Sub

        Public Function GetEmployees() As IEnumerable(Of Employee)
            Return EmployeeManager.GetEmployees()
        End Function
    End Class
End Namespace

我还发现 this 这似乎表明我正在关注步骤正确,但属性未按预期显示

I've also found this which seems to indicate I'm following the correct steps but the properties don't appear as expected

类的公共属性现在显示在数据源"窗口中,可以在其中将它们拖放到报告中.

The public properties of the class now appear in the Data Sources window, where they can be dragged and dropped into the report.

这不会发生 - 属性永远不会出现

This doesn't happen - the properties never appear

EDIT:正如 Alex 所指出的,我需要使用 属性 而不仅仅是任何方法.请参阅下面的 Alex Esselfie 的回答以进行澄清.这仍然没有解决我的问题,但让我更近了一步......

EDIT: As pointed out by Alex, I need to use properties not just any methods. Please see Alex Esselfie's answer below for clarification. This still hasn't solved my problem but has got me a step closer...

推荐答案

从您的描述和随附的代码来看,ReportData 类中没有任何属性.如果我认为正确,该类应该生成要在报告中显示的 IEnumerable.

From your description and accompanying code, there are no properties in the ReportData class. If I reckon well, that class is supposed to be generating the IEnumerable you want to display on the report.

如果是这种情况,您必须在向导中选择 Employee 类作为数据源.这将允许您在报告中显示数据.

If that's the case, you'll have to select the Employee class as the DataSource in the wizard. This will allow you to display the data on your report.

我将处理一个快速项目,稍后将其添加到此答案中.

I'll work on a quick project and add it to this answer in a moment.


编辑 1

Visual Studio 2008

  1. 设计视图中打开报告.
  2. 从菜单栏中选择Data > Show Data Sources
  3. 点击数据源"窗口中的添加新数据源.
  4. 选择对象并点击下一步.
  5. 浏览解决方案树并选择要绑定到的类.
    在您的情况下,您绑定到 Employee 类.
  6. 点击下一步,然后完成.
  1. Open the report in Design View.
  2. Select from the menu bar Data > Show Data Sources
  3. Click Add New Data Source on the Data Sources window.
  4. Select Object and click Next.
  5. Browse the solution tree and select the class you want to bind to.
    In your case, you bind to the Employee class.
  6. Click Next and then Finish.


Visual Studio 2010

  1. 设计视图中打开报告.
  2. 从菜单栏中选择View > Report Data
  3. 在数据源"窗口中单击 New > Dataset....
  4. 输入数据集的名称(例如员工)
  5. 创建新数据源或选择现有数据源.

  1. Open the report in Design View.
  2. Select from the menu bar View > Report Data
  3. Click New > Dataset... on the Data Sources window.
  4. Enter a name for the dataset (e.g. Employee)
  5. Create a New Data source or select an existing data source.

创建新数据源

  • 选择对象并点击下一步.
  • 浏览解决方案树并选择要绑定到的类.
  • 点击完成.


将类绑定到报表后,继续像往常一样设计报表.


After binding a class to the report, go ahead and design your report as usual.

编辑 2

我为您上传了一个示例项目.这是 链接.
如果您需要进一步说明,请通知我,以便我为您提供有关如何复制项目的分步程序.

I uploaded a sample project for you. Here's the link.
If you need further clarification, please notify me so I give you a step by step procedure on how to replicate the project.

这篇关于VB.Net:如何在报表 (.rdlc) 中使用对象数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆