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

6

10

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

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.

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

Thanks!

Omniscience answered 7/8, 2009 at 3:58 Comment(0)
I
12

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.

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.

Idolater answered 7/8, 2009 at 13:57 Comment(1)
Something like this is what I was looking for, thanks Kyle! I think I can build a very small app that gets a sql and writes a xml in order to do this a bit faster.Omniscience
T
5

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

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

Tissue answered 31/3, 2010 at 21:42 Comment(1)
Thanks jalchr, this is what I was looking for. I already solved months ago, but thanks!Omniscience
D
4

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

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

Let me know is you need more guidelines

Dynasty answered 7/8, 2009 at 10:35 Comment(1)
+1 Przemaas for showing me the programatically way of doing this, though I have the feeling that this is a bit conversome, don't you think? You should be able to edit the datasource of a label control directly. Thanks!Omniscience
F
2

Here is an alternate..

rtpObject.DataSourceSchema = dataSet.GetXmlSchema();
Fruitless answered 6/7, 2013 at 10:39 Comment(0)
S
1

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
Skiest answered 20/11, 2013 at 12:42 Comment(0)
P
1
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);
Prewitt answered 23/11, 2013 at 13:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.