Create a ListBox Control that Displays Default Parameters

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

  1. Open the Web or Windows form.

  2. From the View menu, click Designer.

  3. If you are developing a Web Site, do the following:

    • Click the CrystalReportViewer control to select it.
    • Press the LEFT ARROW on your keyboard so that a flashing cursor appears, and then press ENTER.
The CrystalReportViewer control drops by one line.
  1. If you are developing a Windows project, do the following:

    • Click the CrystalReportViewer control to select it.

    • From the Properties window, set the Dock to "Bottom"

      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".

    • Resize the Windows form and the CrystalReportViewer control so that the CrystalReportViewer is large enough to display a report. Leave room above the CrystalReportViewer control for a ListBox control.

    • From the Properties window, set the Anchor to "Top, Bottom, Left, Right."

    • Resize the Windows form and the CrystalReportViewer control so that the CrystalReportViewer is large enough to display a report. Leave room above the CrystalReportViewer control for a ListBox control.

  2. From the Toolbox, drag a ListBox control above the CrystalReportViewer control.

> [!NOTE]
> <P>If a Smart Task appears on the ListBox (when using Visual Studio), press Esc to close it.</P>
  1. Click on the ListBox control to select it.

  2. From the Properties window:

    • Set the ID or Name to "defaultParameterValuesList."
    • Set the SelectionMode to "Multiple" (in a Windows project, "MultiExtended").
  3. From the File menu, select Save All.

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

  1. Open the Web or Windows form.

  2. From the View menu, click Code.

  3. At the bottom of the class, create a new private method named GetDefaultValuesFromParameterField() that returns an ArrayList instance, with ReportDocument passed into the method signature.

``` vb
Private Function GetDefaultValuesFromParameterField(ByVal myReportDocument As ReportDocument) As ArrayList

End Function
```

``` csharp
private ArrayList GetDefaultValuesFromParameterField(ReportDocument reportDocument)
{
}
```
  1. Within the GetDefaultValuesFromParameterField() method, retrieve the ParameterFieldDefinitions indexed class, which comes from the DataDefinition property of the ReportDocument instance.
``` vb
Dim myParameterFieldDefinitions As ParameterFieldDefinitions = myReportDocument.DataDefinition.ParameterFields
```

``` csharp
ParameterFieldDefinitions parameterFieldDefinitions = reportDocument.DataDefinition.ParameterFields;
```
  1. Retrieve the ParameterFieldDefinition instance from the ParameterFieldDefinitions indexed class, which is based on the index entry of the PARAMETER_FIELD_NAME constant.

    Dim myParameterFieldDefinition As ParameterFieldDefinition = myParameterFieldDefinitions(PARAMETER_FIELD_NAME)
    
    ParameterFieldDefinition parameterFieldDefinition = parameterFieldDefinitions[PARAMETER_FIELD_NAME];
    
  2. Retrieve a ParameterValues indexed class (as the variable defaultParameterValues) from the DefaultValues property of the ParameterFieldDefinition instance.

    Dim defaultParameterValues As ParameterValues = myParameterFieldDefinition.DefaultValues
    
    ParameterValues defaultParameterValues = parameterFieldDefinition.DefaultValues;
    
  3. Declare and instantiate an ArrayList.

    Dim myArrayList As ArrayList = New ArrayList()
    
    ArrayList arrayList = new ArrayList();
    
  4. Create a foreach loop, to retrieve each ParameterValue instance from defaultParameterValues.

``` vb
For Each myParameterValue As ParameterValue In defaultParameterValues

Next
```

``` csharp
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 for Visual Studio. Check your API (using IntelliSense) to see which property is available under ParameterValue:
  1. If the available property is IsRange then, within the foreach loop, enter this code:

    If (Not myParameterValue.IsRange) Then
    
    End If
    
    if(!parameterValue.IsRange)
    {
    }
    
  2. Or, if the available property is Kind (DiscreteOrRangeKind, an enum with three values: DiscreteValue, RangeValue, DiscreteAndRangeValue) then, within the foreach loop, enter this code instead:

    If (myParameterValue.Kind = DiscreteOrRangeKind.DiscreteValue) Then
    
    End If
    
    if(parameterValue.Kind == DiscreteOrRangeKind.DiscreteValue)
    {
    }
    
  3. Within this nested conditional block, cast the ParameterValue instance to its extended class, DiscreteParameterValue.

    Dim myParameterDiscreteValue As ParameterDiscreteValue = CType(myParameterValue, ParameterDiscreteValue)
    
    ParameterDiscreteValue parameterDiscreteValue = (ParameterDiscreteValue)parameterValue;
    
  4. Also within the nested conditional block, add the Value property of the ParameterDiscreteValue instance (converted to String) into the ArrayList instance.

    myArrayList.Add(myParameterDiscreteValue.Value.ToString())
    
    arrayList.Add(parameterDiscreteValue.Value.ToString());
    
  5. Outside the conditional block, and outside the foreach loop, at the end of the method, return the ArrayList instance from the method.

    Return myArrayList
    
    return arrayList;
    

To bind the ArrayList returned from the method to the ListBox in a Web project

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.

  1. In the 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.

  2. Within the line breaks, create a Not IsPostBack conditional block.

    If Not IsPostBack Then
    
    End If
    
    if(!IsPostBack)
    {
    }
    

    Note

    The Not IsPostBack conditional block is used to encapsulate code that should only be run the first time the page loads. Controls are typically bound to data values within Not IsPostBack conditional blocks so that their data values (and any subsequent control events) are not reset during page reloads.

  3. Within the 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.

``` vb
defaultParameterValuesList.DataSource = GetDefaultValuesFromParameterField(customersByCityReport)
```

``` csharp
defaultParameterValuesList.DataSource = GetDefaultValuesFromParameterField(customersByCityReport);
```
  1. Still within the Not IsPostBack conditional block, call the DataBind() method of the defaultParameterValuesList ListBox.
``` vb
defaultParameterValuesList.DataBind()
```

``` csharp
defaultParameterValuesList.DataBind();
```

To bind the ArrayList returned from the method to the ListBox in a Windows project

  1. In the 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.

  2. Within the line breaks, set the DataSource property of the defaultParameterValuesList ListBox to the GetDefaultValuesFromParameterField() helper method, passing in the CustomersByCity report instance as a method parameter.

``` vb
defaultParameterValuesList.DataSource = GetDefaultValuesFromParameterField(customersByCityReport)
```

``` csharp
defaultParameterValuesList.DataSource = GetDefaultValuesFromParameterField(customersByCityReport);
```

To test the population of the defaultParameterValuesList ListBox control

You are now ready to build and run the project, to verify whether the defaultParameterValuesList ListBox is populated.

  1. From the Build menu select Build Solution.

  2. If you have any build errors, go ahead and fix them now.

  3. From the Debug menu, click Start.

The defaultParameterValuesList ListBox control displays a complete list of default values (cities, in our tutorial).
  1. Return to Visual Studio and click Stop to exit from debug mode.

In the next section, you add a button to redisplay the report based on selections from the defaultParameterValuesList ListBox control.