Thursday, May 28, 2009

Programmatically/Dynamically building an RDLC

I was designing a Local Report when I stumbled upon a shortcoming of the Report Rendering Engine. A Rectangle has a Hidden property. When you set the Hidden property to True, the content of the Rectangle doesn't render, but the space of the content is preserved. In my case, this caused some white pages. So I looked for a way to build a RDLC programmatically. An RDLC is just an XML, so how hard could it be :).

I first searched for an example on MSDN, but I didn't like the way described there. In this article on MSDN they use a XmlTextWriter, which makes the code hard to read and not very maintainable in my opinion. You can find that MSDN article here.

After some further investigation I found the RDLC XSD on MSDN. You can find the XSD here.

I used the xsd.exe Tool to generate a Class based on the XSD. This way you can create an RDLC in a more Object Oriented way, which leads to more maintanable code.

It would be awesome if someone had the courage to add comments, so Intellisense would be able to help creating RDLCs programmatically.

After adding the Report.cs to your solution, you can start making your own RDLCs programmatically. Note that the ReportViewer needs a Stream to read the RDLC.

I chose not to show my whole implementation of my own ReportGenerator, because it's not really a good example. These steps might get you started though.

1. Create a new Report object.
2. Do whatever you want with the object.. Add textboxes, rectangles..
3. Before reading the Report object into the ReportViewer you need to serialize the object to a Stream. I chose to use a MemoryStream. (Don't forget to set the position of the Stream to 0!)

4. Load the Local Report in the ReportViewer using the LoadReportDefinition method.

And that should be it!


  1. Hey, i know this was two years ago, but i'm facing this same problem right now. Would it be possible to get your class to create the rdlc?

  2. Unfortunately, I don't have it anymore. Sorry for that broken link. You should be able to use xsd.exe to generate the class based on the report xsd that can be found on MSDN ( If you have any problems, let me know.

  3. In the framework i use that exe is not any more, but i'll try to get it or figure it out, thanks anyway.

  4. Thanks for such a nice idea, instead of using XML use the class. But I am finding difficult implementing new Report Class. Do you have some samples with you indicating adding Dataset and the textboxes, since it is taking quite a some time to create RDLC file with some basic fields

  5. I don't have an example. Can't you just do set the Value property of the TextBox before generating the Xml? I don't think you *need* to setup the datasources and bindings persee?