Thursday, March 24, 2011

How do I dynamically modify a report using Crystal Reports?

I want to know I can dynamically modify an existing Crystal Report (using C# and Crystal Reports 2008) so that it can report on fundamentally different data (from the same database) using the same layout. The reason is that we are building an application that allows the user to specify:

  1. A filter so that only the data they are interested in is displayed (i.e., whereclause).
    • [e.g., Only display buildings in NYC]
  2. Specify what tables are displayed
    • [e.g., Show buildings and floors but not rooms]
  3. Specify what fields are displayed
    • [e.g., Show building id, number, name, address and floor number]
  4. Change how a section is grouped
    • [e.g., Group by building use, build year, etc]
  5. Change the the fields used in a chart
    • [e.g., Area by Building Use, Occupancy by Building Use, Area by Building Id]

Because of the flexibility of the UI there are many different kinds of "questions" the user can try to report on and I do not want to create a report for every possible combination. For an example of what I am trying to accomplish see the image below. The report on the left depicts buildings grouped by area. The report on the right depicts buildings grouped by site.

Report Comparison

EDIT:

I will attempt to be using report parameters to modify the report as suggested by Philippe Grondier. I have had success in doing this by suppressing groups. When a group is suppressed the groups slide up to fill the blank area so that everything lines up without blank space. However, the same behavior is not true of individual text objects. When a text object is suppressed the output for that field is an empty space. What I would like to do is suppress a text object such that the entire line is suppressed which would result in all the entries below it "bumping" up by one line. How can I achieve this?

From stackoverflow
  • Depending on the kind of update (and objects concerned by the update), you can use either the free craxdrt.dll (Designer Runtime Library.) or the not free craxddrt.dll (Designer Design and Runtime Library). I forgot the details but you will surely find them on the net.

    Your questions:

    1. Filter: it is possible to update the SQLQueryString of the report object at runtime or to update predeclared parameters so that they will be added to the 'Where' clause. I think adding parameters is limited to the craxddrt dll but updating existing ones can be done with the craxdrt dll. You can use the CR interface to update these parameters, or do it through code: one idea would be to update these values in your client interface (from the active form for example) and update them programmatically. Please check here for an example.
    2. Hidding tables: I don't think that 'hidding tables' makes any sense, as you will need your tables to have all your relations available to calculate your summary values. Thus you can propose to your users the possibility to view/hide details or to view/hide your different grouping levels. This can easily be done with the free craxdrt library and the 'areas' collection/'suppress' method of the report object.
    3. Once again, playing with the different areas of the report seem to be the best solution here
    4. I advise you not to think about it! Changing data order can lead to unpredictable results, specially when you calculate intermediate summaries at the group level. Depending on your requests, you could stick to proposal (2), by displaying of not a specific group on the report, or, for simple forms (1 group only), programmatically update at runtime a parameter that will be used in the group definition formula.
    5. Back to proposal (2): build a complete report will all expected data to be displayed, then hide/show the different sections of the report.

    There is another code example available here, showing you how to change the report connection to a different database/different tables at runtime.

    Ryan Taylor : Thanks! I am going to try proposal (2), using parameters to hide undesired sections; however, I am having difficulties making this work for text fields. Details in the original posting.
    Ryan Taylor : Proposal (2) was the correct solution. Ultimately I created group/detail sections for each text object where the sections where suppressed if the field was blank. Additionally, the text objects where suppressed based on a passed in parameter. This had the effect that I wanted. Thanks for the help!

0 comments:

Post a Comment