Dynamically change Groups in Crystal Report

Crystal Reports groups play an important role while designing and displaying data in a more customized way. In this article, I am going to show you how to change groups in Crystal Report, dynamically. Every group has a Header and Footer section and we can change its values by passing dynamic values from a web application, using the .Net platform.

Crystal report groups can be either static or dynamic, depending upon its requirement. We can define a Static group on fields and its values in the report. Where as a dynamic group or groups is a pre-defined Formula Field and we pass values using a Parameter define in the Parameter Field.

Talking about passing parameters, you must be eager to know how we can pass parameters to a Crystal Report in Asp.Net. Believe me it is very simple.

Assuming that you already have designed the report using Crystal Reports; we will straight away go to the Grouping part. If you are still puzzled about how to create a Crystal Report from the beginning, then I recommend you read this article on creating your first Crystal Report in Asp.Net.

As usual, I will use the Employee details table in my report for grouping. It has five columns and the dynamic grouping will be by either Designation or Department.

Create a Parameter in Crystal Report

Open the report in crystal report (for demo we will use Employee.rpt), and in the Field Explorer section, right click Parameter Fields and create a New parameter. Name the String parameter as ParaGroup. We will not define any Default Values and instead pass the values from an application.

Crystal Report Parmater

Related: Create a PDF file using Crystal Report in Asp.Net

The Formula

Once we are done with the parameter, we will create a formula in which the parameter will be used to set conditions for grouping.

if {?ParaGroup} = … then

Create Formula

This formula will be used in the Group By field using the Group Expert. Right click the Formula Fields under Field Explorer and click the New button. Name the formula as DynamicGroup and set conditions using the Parameter we earlier created.

Crystal Report Formula

Crystal Report Group

Create a group by right clicking the report and choose Insert -> Group… which will open an Insert Group window. Select the DynamicGroup formula from the dropdown list.

Crystal Report Group Selection

Crystal Report Group

Finally, in your application set options for passing the parameters for the group we just defined, and run the application to preview the report.