You are now ready to set two values ("Paris" and "Tokyo") into the City parameter field for the CustomersByCity report.
This involves some coding, which you can separate into the following processes:
ConfigureCrystalReports() method, you add the "Paris" and "Tokyo" parameters to an ArrayList instance and pass in both the report and the ArrayList instance to the helper method to be processed.To create a PARAMETER_FIELD_NAME constant
Private Const PARAMETER_FIELD_NAME As String = "City"
private const string PARAMETER_FIELD_NAME = "City";
You are now ready to create the helper method that adds current values to the parameter in the report.
To create a helper method that adds current values to the parameter in the report
"Imports" [Visual Basic] or "using" [C#] declaration to the top of the class for the System.Collections namespace (if this namespace has not already been declared).Imports System.Collections
using System.Collections;
Note This declaration is needed, to access the ArrayList class.
SetCurrentValuesForParameterField(), with two variables in the method signature: ReportDocument, and ArrayList.
Note Later in this tutorial, if you have used an embedded report, you pass your embedded report class into the ReportDocument method parameter. How is this possible? All embedded report classes in Crystal Reports inherit from the ReportDocument base class.
Private Sub SetCurrentValuesForParameterField(ByVal myReportDocument As ReportDocument, ByVal myArrayList As ArrayList) End Sub
private void SetCurrentValuesForParameterField(ReportDocument reportDocument, ArrayList arrayList)
{
}
Note For the ParameterValues class to be accessible, you must have included an"Imports" [Visual Basic]or"using" [C#]declaration at the top of the code-behind class for the CrystalDecisions.Shared namespace. (You added this declaration in Project Setup.)
Dim currentParameterValues As ParameterValues = New ParameterValues()
ParameterValues currentParameterValues = new ParameterValues();
Note In this method, you retrieve values from the ArrayList. Later you write code that adds values to the ArrayList.
For Each submittedValue As Object In myArrayList Next
foreach(object submittedValue in arrayList)
{
}
Dim myParameterDiscreteValue As ParameterDiscreteValue = New ParameterDiscreteValue()
ParameterDiscreteValue parameterDiscreteValue = new ParameterDiscreteValue();
myParameterDiscreteValue.Value = submittedValue.ToString()
parameterDiscreteValue.Value = submittedValue.ToString();
currentParameterValues.Add(myParameterDiscreteValue)
currentParameterValues.Add(parameterDiscreteValue);
This completes the code within the foreach loop. You place the remaining code (from the steps that follow) after the foreach loop.
Dim myParameterFieldDefinitions As ParameterFieldDefinitions = myReportDocument.DataDefinition.ParameterFields
ParameterFieldDefinitions parameterFieldDefinitions = reportDocument.DataDefinition.ParameterFields;
Dim myParameterFieldDefinition As ParameterFieldDefinition = myParameterFieldDefinitions(PARAMETER_FIELD_NAME)
ParameterFieldDefinition parameterFieldDefinition = parameterFieldDefinitions[PARAMETER_FIELD_NAME];
myParameterFieldDefinition.ApplyCurrentValues(currentParameterValues)
parameterFieldDefinition.ApplyCurrentValues(currentParameterValues);
This step procedure showed you how to create a method that retrieves submitted values from an ArrayList instance and places them as current values into a ParameterFieldDefinition instance. Now, you must call this method before your report is bound to the CrystalReportViewer control, for the report to be aware that it has parameter settings.
To call the SetCurrentValuesForParameterField() method before the report is bound to the CrystalReportViewer control
ConfigureCrystalReports() method, create a couple of line breaks in the code above the line that binds the report to the CrystalReportViewer control.
Within these line breaks, you can now enter additional code that modifies the report before it is bound to the viewer.
Dim myArrayList As ArrayList = New ArrayList()
ArrayList arrayList = new ArrayList();
myArrayList.Add("Paris")
myArrayList.Add("Tokyo")
arrayList.Add("Paris");
arrayList.Add("Tokyo");
SetCurrentValuesForParameterField() method, and pass in the CustomersByCityReport instance, and the ArrayList instance.SetCurrentValuesForParameterField(customersByCityReport, myArrayList)
SetCurrentValuesForParameterField(customersByCityReport, arrayList);
The final line of code in the method is code that binds the report to the CrystalReportViewer control.
You are now ready to build and run your project. It is expected that the report displays successfully because there is now code written to set current values into the parameter field.
To test the loading of the CustomersByCity report
The CustomersByCity report displays successfully, showing listings for customers in Paris and Tokyo.
In the next section, you learn how to retrieve the default values from the parameter field and set those values into a ListBox control. These are used at the end of the tutorial to select new cities dynamically and filter the report based on those newly selected cities.
Continue to Create a ListBox control that displays default parameters.
Tutorials: Reading and Setting Discrete Parameters | Tutorials and Sample Code | Tutorials' Sample Code Directory