NDraskovic
NDraskovic

Reputation: 706

Creating a rdlc report with variable number of columns

I have a problem with creating an .rdlc report. The point of this report is to show the number of passengers on a bus routes (it has the number of passengers for each station on that route). The DataSet is filled with a stored procedure (that I can't change). Data for each route is consisted of 2 parts - the first part has the names of the stations on the specific route (there are columns that represent the number of passengers, but their values are null), and the second part contains the number of passengers for each station. Both this parts are a result of the same stored procedure, and they are displayed in the same table. The problem I'm having is that each route has a different number of stations. So my question is - how can I make a report (or subreport) that would adapt to the number of stations on a specific route (the number of columns should be the same as the number of stations on a route). It is important to mention that routes with different number of stations should be displayed on the same report. I'm using VS 2010 and C# to create the application that generates this reports. Can you help me with this problem? Thanks

Example Unfortunately, I'm not allowed to revile specific data, but I'll try my best to give you necessary informations. The result of the stored procedure has this structure: Route_ID | Departure | DataType | StationName | Passengers1....Passengers61 The report is grouped by Route_ID. DataType value defines if the row contains the name of the stations (the first part of the result) or the numbers of passengers (the second part). For instance if a route has 3 stations and one departure, the group would have 4 rows - first 3 would contain Route_ID, DataType and the StationName (each row has a different StationName, first 2 parameters would be same), other columns would have a null value. The second part consists of Route_ID, DataType (different from the previous part) and values in passenger columns (again, other columns would be null). So I need to have a subreport that would have the same number of columns as the first part has rows. I know this is complicated, but entire problem is very complex.

Upvotes: 2

Views: 2735

Answers (1)

Deruijter
Deruijter

Reputation: 2159

Probably not the answer you want to hear, but it is rather hard to dynamically create columns in RDLC (at least I couldn't get it to work).

If possible I would suggest this (somewhat unclean) solution: Create your table with as much stations as is possible:

BUS | StationA | StationB | StationC | StationD | ..
X9Z | 4 people | 5 people | 0 people | 9 people | ..

Then add a visibility expression to each Station column i.e. iif(StationB > -1, true, false). Columns will then only be shown when the value is set. Off course this will mean that you will have to define all stations in advance (and maintain them). So if you have 50 stations that will be a pain in the behind.

Alternative

If you have a lot of stations I would personally recommend looking for some other report generator such as Crystal Reports (Note I don't have experience with that myself, though it crossed my path often when searching for RDLC solutions).

Format your data

After reading your edit it sounds like your dataset structure isn't very practical. I would suggest filling a custom object which is more in line with what you want to display, and use that in your RDLC.

The following question refers to that: Use custom objects as the source for Microsoft Reports (.rdlc)

Where this link is basically the answer: http://www.gotreportviewer.com/objectdatasources/index.html

Upvotes: 1

Related Questions