views:

3364

answers:

3

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!

+2  A: 

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

Przemaas
+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!
Sebastian
+4  A: 

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.

Kyle Gagnet
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.
Sebastian
+2  A: 

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 http://www.devexpress.com/Help/?document=XtraReports/CustomDocument3851.htm&levelup=true

jalchr
Thanks jalchr, this is what I was looking for. I already solved months ago, but thanks!
Sebastian