C# 如何在设计时没有数据集的情况下为 XtraReports 中的字段设置数据源?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1242825/
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-06 14:07:10  来源:igfitidea点击:

How to set datasource for fields in XtraReports without having a dataset at design time?

c#devexpressxtrareport

提问by Sebastian

I'm taking a look now to XtraReports reporting tool and there's something that I don't get it yet.

我现在正在查看 XtraReports 报告工具,但有些东西我还没有弄明白。

How do I set the data source for a certain field (showed in the report as a Label I guess), without having to build a connection, adapter and dataset at design time but doing it programatically.

如何为某个字段设置数据源(我猜在报告中显示为标签),而不必在设计时构建连接、适配器和数据集,而是以编程方式进行。

For example, I can have a table called "User" with 3 fields: UserId, Username and Password. In the report designer I place 3 labels (and here's my question) set the datasource for showing the 3 database fields. Then, in the code behind, I create a connection, execute a command, fill a dataset, create a report instance, pass the datatable to it and show the report preview.

例如,我可以有一个名为“用户”的表,其中包含 3 个字段:用户 ID、用户名和密码。在报表设计器中,我放置了 3 个标签(这是我的问题)设置数据源以显示 3 个数据库字段。然后,在后面的代码中,我创建一个连接,执行一个命令,填充一个数据集,创建一个报表实例,将数据表传递给它并显示报表预览。

Is this possible? Let me know if it isn't clear enough.

这可能吗?如果还不够清楚,请告诉我。

Thanks!

谢谢!

采纳答案by Kyle Gagnet

You could set your Report's DataSourceSchema property to an XML schema that represents your DataSource. That will let you use the Report Designer to set your data bindings at design time without establishing a connection to the database each time.

您可以将报表的 DataSourceSchema 属性设置为代表您的数据源的 XML 架构。这将允许您使用报表设计器在设计时设置数据绑定,而无需每次都建立到数据库的连接。

Here's how I do it: Once I have my report query mostly finalized, I run the code once with a call to

我是这样做的:一旦我的报告查询大部分完成,我运行一次代码并调用

myDataSet.WriteXml("C:\myDataSourceSchema.xml", System.Data.XmlWriteMode.WriteSchema)

Then in the report designer I set the Report's DataSourceSchema property to the newly created file. This will populate the Report Designer's Field List tab so you can bind at design time. That way you only have to have a valid data source once (or any time you change your columns). You can definitely still do Przemaas's approach and do all of your data bindings in code, but I prefer to let the designer handle most of the work.

然后在报表设计器中,我将报表的 DataSourceSchema 属性设置为新创建的文件。这将填充报表设计器的字段列表选项卡,以便您可以在设计时进行绑定。这样,您只需拥有一次有效的数据源(或在您更改列的任何时候)。您绝对仍然可以使用 Przemaas 的方法并在代码中完成所有数据绑定,但我更愿意让设计人员处理大部分工作。

回答by Przemaas

Yes, it is possible. You can define necessary databindings in the code:

对的,这是可能的。您可以在代码中定义必要的数据绑定:

this.xrLabel1.DataBindings.Add(new DevExpress.XtraReports.UI.XRBinding("Text", data, "Name", "aaa"));
  • Text here is property on the XrLabel class. I assume that you want to display bound field as text in label.
  • data is your object with data
  • "Name" is name of field that you want to display
  • "aaa" is display format, applicable in case you want to display values with custom formatting
  • 此处的文本是 XrLabel 类的属性。我假设您想在标签中将绑定字段显示为文本。
  • 数据是你的数据对象
  • “名称”是您要显示的字段名称
  • “aaa”是显示格式,适用于您想使用自定义格式显示值的情况

Basically databindings in XtraReport act pretty much the same way as standard windows forms databindings.

基本上,XtraReport 中的数据绑定与标准窗口表单数据绑定的行为方式几乎相同。

Let me know is you need more guidelines

让我知道您是否需要更多指南

回答by Jalal El-Shaer

Building a report without a dataset, you would use an IList object ... so follow this nice tutorial

在没有数据集的情况下构建报告,您将使用 IList 对象......所以请遵循这个不错的教程

How to: Bind a Web Report to an Array Listhttps://documentation.devexpress.com/#XtraReports/CustomDocument3851

如何:将 Web 报告绑定到数组列表https://documentation.devexpress.com/#XtraReports/CustomDocument3851

回答by Appyks

Here is an alternate..

这是一个替代..

rtpObject.DataSourceSchema = dataSet.GetXmlSchema();

回答by Vinayak

before doing this set modifier property as public

在执行此设置修饰符属性为公共之前

InvoicePrinting_Rpt InvoicePrintingRpt = new InvoicePrinting_Rpt();//report object 

InvoicePrintingRpt.BillDetails.Report.DataSource = ds_Invoice;
InvoicePrintingRpt.Report.DataMember = ds_Invoice.Tables[0].TableName;
 //bellow third parameter as your column name.
InvoicePrintingRpt.lbl_BillHead.DataBindings.Add("Text", null, "BILL_DESCRIPTION");
InvoicePrintingRpt.lbl_Det_Date.DataBindings.Add("Text", null, "TRANSACTION_DATE");
InvoicePrintingRpt.lbl_ISINCode.DataBindings.Add("Text", null, "ISIN_CODE");

ReportViewer1.Report = InvoicePrintingRpt;//assign report obj   
ReportViewer1.Report.Name = "DevExpress_Reports.InvoicePrinting_Rpt";
ReportViewer1.DataBind(); //binding

回答by Donot Don't

XRBinding binding = new XRBinding("Text", ageingBindingSource, "ageing_contactsLookup.name");
this.xrLabel19.DataBindings.Add(binding);

// or //

// 或者 //

XRBinding binding = new XRBinding("Text", dbaDataSet, "transactions.fk_transitems_transactionid.name2");
this.xrTableCell1.DataBindings.Add(binding);