Question

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!

Was it helpful?

Solution

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.

OTHER TIPS

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

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

Here is an alternate..

rtpObject.DataSourceSchema = dataSet.GetXmlSchema();

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
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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top