The remainder of the tutorial is concerned with displaying a complete list of default values for the parameter field in a ListBox control, and based on selections that you make from that ListBox control, refiltering the contents of the report.
In this section you learn how to populate the ListBox control from the default values of the parameter field.
Note Remember that you set the Default Values, a large list of cities, when you created this report at the beginning of the tutorial.
To do this, you must add and configure a ListBox control, and then create a helper method to populate the ListBox control.
To create and configure a ListBox control on the form
The CrystalReportViewer control drops by one line.
Note In Visual Studio, When you select the Dock property, a frame appears instead of a list of options. Select the portion of the frame that corresponds to "Bottom".
Note If a Smart Task appears on the ListBox (when using Visual Studio 2005), press Esc to close it.
You are now ready to create a helper method that retrieves the default values from the parameter field.
To create a helper method that retrieves the default values from the parameter field
GetDefaultValuesFromParameterField() that returns an ArrayList instance, with ReportDocument passed into the method signature.Private Function GetDefaultValuesFromParameterField(ByVal myReportDocument As ReportDocument) As ArrayList End Function
private ArrayList GetDefaultValuesFromParameterField(ReportDocument reportDocument)
{
}
GetDefaultValuesFromParameterField() method, retrieve the ParameterFieldDefinitions indexed class, which comes from the DataDefinition property of the ReportDocument instance.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];
Dim defaultParameterValues As ParameterValues = myParameterFieldDefinition.DefaultValues
ParameterValues defaultParameterValues = parameterFieldDefinition.DefaultValues;
Dim myArrayList As ArrayList = New ArrayList()
ArrayList arrayList = new ArrayList();
For Each myParameterValue As ParameterValue In defaultParameterValues Next
foreach(ParameterValue parameterValue in defaultParameterValues)
{
}
Within the foreach loop, you now create a nested conditional block that checks for discrete (as opposed to range) parameter values. Two versions of this conditional block exist, because the API has changed slightly across versions of Crystal Reports. Check your API (using IntelliSense) to see which property is available under ParameterValue:
If (Not myParameterValue.IsRange) Then End If
if(!parameterValue.IsRange)
{
}
If (myParameterValue.Kind = DiscreteOrRangeKind.DiscreteValue) Then End If
if(parameterValue.Kind == DiscreteOrRangeKind.DiscreteValue)
{
}
Dim myParameterDiscreteValue As ParameterDiscreteValue = CType(myParameterValue, ParameterDiscreteValue)
ParameterDiscreteValue parameterDiscreteValue = (ParameterDiscreteValue)parameterValue;
myArrayList.Add(myParameterDiscreteValue.Value.ToString())
arrayList.Add(parameterDiscreteValue.Value.ToString());
Return myArrayList
return arrayList;
You have retrieved the default values from the parameter field and returned them from the method as an ArrayList. You now bind this ArrayList to the defaultParameterValuesList ListBox control.
Your code varies slightly depending on whether you use a Web project or a Windows project; therefore, be sure to only complete either the Web or Windows procedure below.
To bind the ArrayList returned from the method to the ListBox in a Web project
ConfigureCrystalReports() method, create a couple of line breaks in the code immediately after the line of code that adds the Tokyo string value to ArrayList instance.
Within these line breaks, you can now enter additional code that sets the data source for the defaultParameterValuesList ListBox control when the page loads for the first time.
Not IsPostBack conditional block.If Not IsPostBack Then
End If
if(!IsPostBack)
{
}
Note TheNot IsPostBackconditional block is used to encapsulate code that should only be run the first time the page loads. Controls are typically bound to data values withinNot IsPostBackconditional blocks so that their data values (and any subsequent control events) are not reset during page reloads.
Not IsPostBack conditional block, set the DataSource property of the defaultParameterValuesList ListBox to the GetDefaultValuesFromParameterField() helper method, passing in the CustomersByCity report instance as a method parameter.defaultParameterValuesList.DataSource = GetDefaultValuesFromParameterField(customersByCityReport)
defaultParameterValuesList.DataSource = GetDefaultValuesFromParameterField(customersByCityReport);
Not IsPostBack conditional block, call the DataBind() method of the defaultParameterValuesList ListBox.defaultParameterValuesList.DataBind()
defaultParameterValuesList.DataBind();
To bind the ArrayList returned from the method to the ListBox in a Windows project
ConfigureCrystalReports() method, create a couple of line breaks in the code immediately after the line of code that adds the Tokyo string value to ArrayList instance.
Within these line breaks, you can now enter additional code that sets the data source for the defaultParameterValuesList ListBox control when the page loads for the first time..
GetDefaultValuesFromParameterField() helper method, passing in the CustomersByCity report instance as a method parameter.defaultParameterValuesList.DataSource = GetDefaultValuesFromParameterField(customersByCityReport)
defaultParameterValuesList.DataSource = GetDefaultValuesFromParameterField(customersByCityReport);
You are now ready to build and run the project, to verify whether the defaultParameterValuesList ListBox is populated.
To test the population of the defaultParameterValuesList ListBox control
The defaultParameterValuesList ListBox control displays a complete list of default values (cities, in our tutorial).
In the next section, you add a button to redisplay the report based on selections from the defaultParameterValuesList ListBox control.
Continue to Setting Parameters from ListBox Selections.
Tutorials: Reading and Setting Discrete Parameters | Tutorials and Sample Code | Tutorials' Sample Code Directory