Editorials

The Report Viewer Control

Today I was talking with a friend about the Report Viewer web control that ships with visual studio. The Report Viewer control is a multi-purpose tool for presenting RDL defined reports in a web site. What makes it really cool is the fact that it is truly multi purpose.

Right out of the box it has some really cool features many of us have come to rely on, such as the ability to export a report to a number of different types of files. You probably already use the export to PDF, Excel, Text and other file types. That in itself is golden.

Many folks know you can use this control along with SSRS hosted reports or as a tool to build reports in your application by connecting the control with an RDL definition file and a data source. However, what you may not know is that you can build a data source on the fly as well.

I always wanted to know how you could use reports when you don’t have an ADO data source to support your report definition. It turns out that you can assign your data source at run time, as you may already know. However, your data source may be an in memory ADO.Net DataTable object.

So, if your data does not come from a data source that is naturally available through a DataTable, say it is a collection of custom classes in an IEnumerable or ICollection based object, then you can adapt the data into a DataTable you create yourself. You can programmatically create a new DataTable, Define the columns, and add rows. So if you enumerate through your collection of objects, you can transform them into data rows for your data table, and insert them in a new DataTable you define in memory. Once the DataTable is complete you assign it to your report control instance and have it render the results. Now you have a report that was created from a non-ado source, through the adaptation of your objects to a data source the engine recognizes.

Perhaps some of you know how to do this by using the dot net collection as a data source directly for the report definition. I wouldn’t be surprised if this is possible, and I have not come across it yet.

So, don’t give up on your reporting tool simply because you are working with objects. It is still possible to use the power of the RDL report definitions, through adapting your objects to an ADO.Net DataTa ble.

Cheers,

Ben