Home / Posts tagged "winforms"

ActiveReports and POCO data sources

I was recently working on a WinForms project that was pulling data from a RSS feed. Since all of the data was loaded from online and it was a relatively simple addition there was no need for a local database. The only problem came when I was tasked with using ActiveReports to create a report of this data, since ActiveReports expects things like DataTables, DataSets and you can’t have a POCO (Plain Old C# Object) act as a data source directly. Most online tutorials suggested making a fake DataTable from your objetcs and looping through your collection manually to add fields and rows, which seems somewhat tedious if you’re working with a large collection of possible fields. Fortunately ActiveReports supports XML data sources, so if your data is Serializable you can just plop it in as a data source, like so:

DataDynamics.ActiveReports.DataSources.XMLDataSource ds = new DataDynamics.ActiveReports.DataSources.XMLDataSource();
ds.FileURL = null;
ds.RecordsetPattern = "//Entry"; // or whatever your data is serialized as
var sw = new StringWriter();
var ser = new XmlSerializer(t);
ser.Serialize(sw, obj);
ds.LoadXML(sw.ToString());
this.DataSource = ds;

Once that’s done you can go about data binding your report fields to your POCO data memebers.