C# 将 RDLC 数据源设置为对象的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11038288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 16:13:32  来源:igfitidea点击:

Issues Setting RDLC Datasource to Object

c#asp.net-mvcreportviewerrdlc

提问by War2d2

Background:

背景:

I've been tasked with converting an Access database application to ASP.Net C# MVC. This is my first MVC application.

我的任务是将 Access 数据库应用程序转换为 ASP.Net C# MVC。这是我的第一个 MVC 应用程序。

There are 10 reports that need converting. We're using RDLC files and reportviewer. I'm using Visual Studio 2010 with all the most recent patches. We're hooking up to a SQL Server database that's been populated by the existing Access database, so the table structure is pretty much set in stone, or else we'll have to try to convert 10 years of data.

有 10 个报告需要转换。我们正在使用 RDLC 文件和报告查看器。我正在使用带有所有最新补丁的 Visual Studio 2010。我们正在连接到由现有 Access 数据库填充的 SQL Server 数据库,因此表结构几乎是一成不变的,否则我们将不得不尝试转换 10 年的数据。

I've completed all but two of the reports. The final reports require more processing, and don't just display data from the database. The easiest way to do this would be to create a C# object and do all the processing server-side, then use RDLC to display the results.

我已经完成了除了两份报告之外的所有报告。最终报告需要更多的处理,而不仅仅是显示数据库中的数据。最简单的方法是创建一个 C# 对象并在服务器端进行所有处理,然后使用 RDLC 显示结果。

Issue

问题

The problem I'm having is that Visual Studio won't recognize the objects I've created as potential datasources. Every time I try to "add dataset" it brings up the "datasource configuration wizard" and only offers the SQL Server database as the possible data connection. I know there's a screen that exists that allows you to select an object as your dataset, but I never see that screen.

我遇到的问题是 Visual Studio 无法将我创建的对象识别为潜在的数据源。每次我尝试“添加数据集”时,它都会弹出“数据源配置向导”,并且只提供 SQL Server 数据库作为可能的数据连接。我知道有一个屏幕可以让你选择一个对象作为你的数据集,但我从来没有看到那个屏幕。

Here are the objects (processing functions removed for clarity):

以下是对象(为清楚起见,删除了处理功能):

public class TurnAroundVal
{
    // Registration Package information
    public string dataType { get; set; }

    // Calculated totals; values only set through constructor or calculation function
    public int packageCount { get; private set; }
    public int dayCount { get; set; }
    public double avgTurnAround { get; private set; }
    public int upperRange { get; private set; }
    public int lowerRange { get; private set; }
}

public class TurnAroundVals
{
    // Public Variables
    public IEnumerable<TurnAroundVal> TurnArounds { get; private set; }
    public DatePass dates { get; set; }
    public int pkgTotal { get; private set; }
    public double dayTotal { get; private set; }
    public double avgAllTurnArounds { get; private set; }
}

I also would be willing to use the IEnumerable of "TurnAroundVal" as a datasource, and just pass the dates, int, and doubles in as parameters. Either would work.

我也愿意使用“TurnAroundVal”的 IEnumerable 作为数据源,并只将日期、整数和双精度作为参数传递。要么会工作。

Question

Is there a setting in Visual Studio 2010 that I'm missing to allow the RDLC Designer to see the objects I created? Am I wrong in even thinking this will work with the classes I defined?

我在 Visual Studio 2010 中是否缺少允许 RDLC 设计器查看我创建的对象的设置?我什至认为这将适用于我定义的类,我错了吗?

Answer

回答

All of the suggestions given were helpful, but ultimately what got it to work for me was to create a temporary non-web project in the same solution and create the RDLC there. When I went to add a dataset, it shows "Objects" as an option. You have to add a reference to your web project so that your objects are visible, but then you can pick the one you want and use that in the RDLC. After that, you can just drag-drop the RDLC file into your web project, delete the temp project, and you're good to go. Just remember that you have to add the datasources manually in code, and they have to be named the same as you specified in the RDLC.

给出的所有建议都很有帮助,但最终让它对我有用的是在同一个解决方案中创建一个临时的非 Web 项目并在那里创建 RDLC。当我去添加数据集时,它显示“对象”作为选项。您必须添加对 Web 项目的引用,以便您的对象可见,然后您可以选择所需的对象并在 RDLC 中使用它。之后,您只需将 RDLC 文件拖放到您的 Web 项目中,删除临时项目,就可以了。请记住,您必须在代码中手动添加数据源,并且它们的名称必须与您在 RDLC 中指定的名称相同。

采纳答案by Marnee KG7SIO

  1. Make sure the classes are in the same namespace as the application.
  2. Try building the application before you create the report
  3. Start the report wizard. Under DataSouce select the name of your web application.
  4. Under Available Datasets you should see what Visual Studio interprets to be your "Select Method". If all is well, this should be TurnArounds.
  1. 确保这些类与应用程序位于相同的命名空间中。
  2. 在创建报告之前尝试构建应用程序
  3. 启动报告向导。在 DataSouce 下选择您的 Web 应用程序的名称。
  4. 在可用数据集下,您应该看到 Visual Studio 解释为您的“选择方法”的内容。如果一切顺利,这应该是 TurnArounds。

You may need to put the class file in the App_Data or App_Code folder, but I am not certain.

您可能需要将类文件放在 App_Data 或 App_Code 文件夹中,但我不确定。

This might also help.

这也可能有帮助。

http://msdn.microsoft.com/en-us/library/ms251692%28v=vs.100%29.aspx

http://msdn.microsoft.com/en-us/library/ms251692%28v=vs.100%29.aspx

回答by raphael

Set your objects under the same namespace but on a different project which you then reference on your UI project.

将您的对象设置在相同的命名空间下,但位于不同的项目上,然后在您的 UI 项目中引用该项目。

Also try implementing INotifyPropertyChanged.

也尝试实施INotifyPropertyChanged.

回答by Markus - YnotBetter.com

Visual Studio does not recognize your class when you use only public members. When you use getter and setter instead, you can see your class when you choose your datasource.

当您仅使用公共成员时,Visual Studio 无法识别您的类。当您改用 getter 和 setter 时,您可以在选择数据源时看到您的类。

There are some other wizards in Visual Studio that do not work with public members.

Visual Studio 中还有一些其他向导不适用于公共成员。

Cheers, Markus

干杯,马库斯

回答by tomRedox

I've just come across this same issue when trying to create RDLC reports in an ASP.NET MVC project in Visual Studio 2017, so I'm adding this as a separate answer to make it clear this is still an issue at January 2018.

我刚刚在Visual Studio 2017的 ASP.NET MVC 项目中尝试创建 RDLC 报告时遇到了同样的问题,因此我将其添加为单独的答案,以明确这在 2018 年 1 月仍然是一个问题。

My solution consisted of a C# library project and an MVC client project.

我的解决方案包括一个 C# 库项目和一个 MVC 客户端项目。

Adding reports (by choosing at add a new item, then choosing a Report Wizard item) in the C# library projectbrings up the Report Wizard with the Data Source Configuration Wizard (where I can choose whether to use a Database, Service or an Object as the datasource) modally on top of it:

在 C# 库项目中添加报告(通过选择添加一个新项目,然后选择一个报告向导项目)会打开带有数据源配置向导的报告向导(我可以在其中选择是使用数据库、服务还是对象作为数据源)以模态方式位于其之上:

Report Wizard showing Data Source Configuration Wizard

显示数据源配置向导的报表向导

Whereas, choosing to add a new report wizard item in the MVC projectjust brought up the Report Wizard without the Data Source Configuration Wizard:

然而,选择在 MVC 项目中添加一个新的报告向导项目只是带来了没有数据源配置向导的报告向导:

Report Wizard without the Data Source Configuration Wizard

没有数据源配置向导的报表向导

I tried adding business object classes into my MVC project, recompiling it and then adding a Report to the MVC Project again, but I still got the second screenshot, so it seems like you just don't get the Data Source Configuration Wizard when adding to MVC projects for some reason.

我尝试将业务对象类添加到我的 MVC 项目中,重新编译它,然后再次向 MVC 项目添加一个报告,但我仍然得到了第二个屏幕截图,所以看起来你在添加到时没有获得数据源配置向导MVC 项目出于某种原因。

回答by Shane.A

I was able to add an object as a datasource in VS 2017 without the report Wizard. [Winforms app using c#] [PS I am a NOOB, so there is going to be mistakes etc, but i hope this helps with giving a direction.] steps I used:

我能够在没有报表向导的情况下在 VS 2017 中添加一个对象作为数据源。[使用 c# 的 Winforms 应用程序] [PS 我是一个菜鸟,所以会有错误等,但我希望这有助于提供方向。] 我使用的步骤:

part A 1. create an object that represents the data e.g.: object / model class in my case it is an appointment object

A 部分 1. 创建一个表示数据的对象,例如:对象/模型类在我的例子中它是一个约会对象

 class FutureVisitsModel
    {
        public DateTime StartDate {get; set;}
        public string Client_Id { get; set; }
        public string Treatment_Desc { get; set; }
        public string Service_Code { get; set; }

        public FutureVisitsModel()
        {
            DateTime startdate = StartDate;
            String clinetid = Client_Id;
            String treatmentdesc = Treatment_Desc;
            String serviceCode = Service_Code;
        }
    }

Part B: create report 2. Added report to solution: right clicked solution, select add new item and chose report enter image description here3. opened the blank report 4. from toolbox, drag n drop a table to the report 5. a dialogue opens to choose a data source 6. select the object from your solution [ ypu will be looking for the class you created in part A

B 部分:创建报告 2. 将报告添加到解决方案:右键单击解决方案,选择添加新项目并选择报告 在此处输入图像描述3. 打开空白报告 4. 从工具箱中,将表格拖放到报告中 5. 一个对话框打开以选择数据源 6. 从您的解决方案中选择对象 [ypu 将查找您在 A 部分创建的类

Part C: create a report viewer form 7. on solution explorer create a new form "formRptViewer" 8. open the form and add report viewer control 9. If you dont have the control in your tool box, you will need to install the report viewer package from nugget or install via package manger console. webforms version : Install-Package Microsoft.ReportingServices.ReportViewerControl.WebForms winforms version: Install-Package Microsoft.ReportingServices.ReportViewerControl.WinForms

C 部分:创建报表查看器表单 7. 在解决方案资源管理器中创建一个新表单“formRptViewer” 8. 打开表单并添加报表查看器控件 9. 如果您的工具箱中没有该控件,则需要安装该报表来自 nugget 的查看器包或通过包管理器控制台安装。webforms 版本:Install-Package Microsoft.ReportingServices.ReportViewerControl.WebForms winforms 版本:Install-Package Microsoft.ReportingServices.ReportViewerControl.WinForms

9.1 set the control of the report viewer to public in solution explorer click the dropdown next to your formRptViewer to see the files that make up the form edit this file: formRptViewer.Designer.cs change the controll pf the reprtviewer to public public Microsoft.Reporting.WinForms.ReportViewer ReportViewer1;

9.1 在解决方案资源管理器中将报表查看器的控件设置为 public 单击 formRptViewer 旁边的下拉列表以查看构成表单的文件 编辑此文件:formRptViewer.Designer.cs 将 reprtviewer 的控件更改为 public public Microsoft.Reporting .WinForms.ReportViewer ReportViewer1;

part D: create a datatable and send it to report 10. create a datatable DataTable dataTableFutureVisits = new DataTable(); [you will need to fill the datatable with y our own data] 11. add using statement using Microsoft.Reporting.WinForms; 12. set the data source and create a new instance of the report viewer

D部分:创建数据表并发送给报表 10.创建数据表 DataTable dataTableFutureVisits = new DataTable(); [您需要使用我们自己的数据填充数据表] 11. 使用 Microsoft.Reporting.WinForms 添加 using 语句;12.设置数据源并新建报表查看器实例

ReportDataSource rs = new ReportDataSource();
            rs.Name = "DataSet1";
            rs.Value = dataTableFutureVisits;
            FormRptViewer frm = new FormRptViewer();
            frm.ReportViewer1.LocalReport.DataSources.Clear();
            frm.ReportViewer1.LocalReport.DataSources.Add(rs);
            frm.ReportViewer1.LocalReport.ReportEmbeddedResource = "ChiroRecallList.RptFutureVisits.rdlc";
            // name the report with date
            frm.ReportViewer1.LocalReport.DisplayName = "Your File Name Goes Here" + "_" + DateTime.Now.ToString("yyyyMMdd HH:mm");
            frm.ShowDialog();

Part E: updating the report: adding columns to the datatable and show them in the report [ also forks for removing a column]

E 部分:更新报告:向数据表中添加列并在报告中显示它们 [也用于删除列的分叉]

  1. add a column to your datatable
  2. change your object class to reflect the data [ as in part A ]
  3. open the report and delete the dataset [not data source ]
  4. in solution explorer: there is a properties dropdown expand that
  5. expand the datasources folder
  6. delete the data source that is named after your model screen shot

  7. clean and rebuild solution screenshot

  8. open the report and right click on your datasource and add new dataset screen shot
  1. 在数据表中添加一列
  2. 更改您的对象类以反映数据 [如 A 部分]
  3. 打开报表并删除数据集[非数据源]
  4. 在解决方案资源管理器中:有一个属性下拉扩展
  5. 展开数据源文件夹
  6. 删除以您的模型屏幕截图命名的数据源

  7. 清理并重建解决方案 屏幕截图

  8. 打开报告并右键单击您的数据源并添加新的数据集 屏幕截图