VB.Net:如何在报表 (.rdlc) 中使用对象数据源

发布于 2024-09-19 08:47:09 字数 2216 浏览 9 评论 0 原文

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

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

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

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

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

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

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

当我尝试向报表添加表并提示我选择数据集时,ReportData 数据源未显示在数据源下拉列表中。

我缺少什么?有人可以指出我正确的方向吗?

我的简单 ReportData 对象:

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 这似乎表明我遵循正确的步骤,但属性未按预期显示

该类的公共属性现在显示在“数据源”窗口中,可以将它们拖放到报表中。

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

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

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

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

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.

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.

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 also appears in the Data Sources pane - It has a > next to it but when I expand it, it has no children.

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.

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?

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

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: 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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

弱骨蛰伏 2024-09-26 08:47:09

根据您的描述和随附的代码,ReportData 类中没有属性。如果我猜得好,该类应该生成您想要在报告上显示的 IEnumerable 。

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

我将开展一个快速项目,并稍后将其添加到此答案中。


编辑 1

如何将类绑定到报表

Visual Studio 2008

  1. 设计视图中打开报表。
  2. 从菜单栏中选择Data> 显示数据源
  3. 单击“数据源”窗口上的添加新数据源
  4. 选择对象,然后单击下一步
  5. 浏览解决方案树并选择要绑定到的类。
    在您的情况下,您绑定到 Employee 类。
  6. 单击下一步,然后单击完成

Visual Studio 2010

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

    创建新数据源

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

将类绑定到报告后,继续像往常一样设计您的报告。


编辑2

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

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.

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.


Edit 1

How to Bind a Class to a Report

Visual Studio 2008

  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. 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.

    Creating a New Data source

    • Select Object and click Next.
    • Browse the solution tree and select the class(es) you want to bind to.
    • Click Finish.

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


Edit 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.

有深☉意 2024-09-26 08:47:09

我也有同样的问题。我必须向表示层添加一个数据源(对象类型),该数据源指向业务层中的对象(具有属性类的集合)。我在表示层中创建了报告(.rdlc)并将其指向数据源并且一切正常。这是我发现向报告公开字段属性的唯一方法。在运行时,我将报告数据源绑定到集合。我喜欢在业务层设计一个“平面”集合,处理数据并将其发送到 rdlc,使报告尽可能“哑”。

I had the same problem. I had to add a Data Source (Object Type) to my presentation layer that pointed to my Object in the Business layer (collection with property class). I created the report (.rdlc) in the presentation layer and pointed it to the Data Source and everthing working fine. This was the only way I found to expose the field properties to the report. At runtime I bind the report datasource to the collection. I like to design a "flat" collection in the business layer, process the data and send it to the rdlc keeping the report as "dumb" as possible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文