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

Sebastian picture Sebastian · Aug 7, 2009 · Viewed 54.4k times · Source

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!

Answer

Kyle Gagnet picture Kyle Gagnet · Aug 7, 2009

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.