By default, the parameters are of the following data types:
- String. For entering alphanumeric text.
- Currency. For entering a number with two decimal places.
- Date. For entering a date in the format Month/Day/Year.
- DateTime. For entering a date as well as time in the format Month/Day/Year/ Hour/Minute/Second/AM or PM
- Number. For entering a number with variable decimal places.
- Boolean. For prompting the users for true or false response.
When you create a parameter, you can also define a prompting text that appears whenever the Parameter Field dialog appears. Once you have created a parameter field, then the standard notation for this field is the field name enclosed in curly brackets preceded by the question mark, that is, {?ParameterField}.
In order to create a parameter to used in the report, right-click on the Parameter Fields node in the Field Explorer windows and select New. The Create Parameter Field window will appear. You can type a parameter name, prompting text and choose data type (see Figure 2).
Figure 2
Let's add a range parameter field to introduce a department identifier range (see Figure 3). Now you can drag and drop these two parameter fields onto the report design surface and run the application.
Figure 3
When the application is running, then you're prompted for entering the parameter fields. For the Caption parameter field, we have (see Figure 4).
Figure 4
For the Department Identifier range parameter field, we have the following figure (see Figure 5).
Figure 5
Now we're going to start by looking the implementation of setting value to parameter fields. When you bind the report item with the CrystalReportViewer control, a new object is added to the form design surface. This object represents an instance of the report. Set this object name to m_rptParameterFields. Parameters within a report are contained within the collection named ParameterFields. You can add programmatically new, configure and delete parameters.
In order to set a value to the ReportCaption parameter field, you must use the following code (see Listing 1).
ParameterValues pvValues = new ParameterValues();
ParameterDiscreteValue pdvDiscreteValue = new ParameterDiscreteValue();
pdvDiscreteValue.Value = "This is my first report caption.";
pvValues.Add(pdvDiscreteValue);
ParameterFieldDefinitions pfdDefinitions = this.m_rptParameterFields.DataDefinition.ParameterFields;
ParameterFieldDefinition prdDefinition = pfdDefinitions["ReportCaption"];
prdDefinition.ApplyCurrentValues(pvValues);
this.m_crvCrystalReportViewer.Refresh();