Crystal Reports XI Release 2  

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.

  4. 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."
  5. From the Toolbox, drag a ListBox control above the CrystalReportViewer control.
    Note   If a Smart Task appears on the ListBox (when using Visual Studio 2005), press Esc to close it.
  6. Click on the ListBox control to select it.
  7. From the Properties window:
    • Set the ID or Name to "defaultParameterValuesList."
    • Set the SelectionMode to "Multiple" (in a Windows project, "MultiExtended").
  8. 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.
    [Visual Basic]
    Private Function GetDefaultValuesFromParameterField(ByVal myReportDocument As ReportDocument) As ArrayList
    
    End Function
    [C#]
    private ArrayList GetDefaultValuesFromParameterField(ReportDocument reportDocument)
    {
    }
  4. Within the GetDefaultValuesFromParameterField() method, retrieve the ParameterFieldDefinitions indexed class, which comes from the DataDefinition property of the ReportDocument instance.
    [Visual Basic]
    Dim myParameterFieldDefinitions As ParameterFieldDefinitions = myReportDocument.DataDefinition.ParameterFields
    [C#]
    ParameterFieldDefinitions parameterFieldDefinitions = reportDocument.DataDefinition.ParameterFields;
  5. Retrieve the ParameterFieldDefinition instance from the ParameterFieldDefinitions indexed class, which is based on the index entry of the PARAMETER_FIELD_NAME constant.
    [Visual Basic]
    Dim myParameterFieldDefinition As ParameterFieldDefinition = myParameterFieldDefinitions(PARAMETER_FIELD_NAME)
    [C#]
    ParameterFieldDefinition parameterFieldDefinition = parameterFieldDefinitions[PARAMETER_FIELD_NAME];
  6. Retrieve a ParameterValues indexed class (as the variable defaultParameterValues) from the DefaultValues property of the ParameterFieldDefinition instance.
    [Visual Basic]
    Dim defaultParameterValues As ParameterValues = myParameterFieldDefinition.DefaultValues
    [C#]
    ParameterValues defaultParameterValues = parameterFieldDefinition.DefaultValues;
  7. Declare and instantiate an ArrayList.
    [Visual Basic]
    Dim myArrayList As ArrayList = New ArrayList()
    [C#]
    ArrayList arrayList = new ArrayList();
  8. Create a foreach loop, to retrieve each ParameterValue instance from defaultParameterValues.
    [Visual Basic]
    For Each myParameterValue As ParameterValue In defaultParameterValues
    
    Next
    [C#]
    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:

  9. If the available property is IsRange then, within the foreach loop, enter this code:
    [Visual Basic]
    If (Not myParameterValue.IsRange) Then
    
    End If
    [C#]
    if(!parameterValue.IsRange)
    {
    }
  10. 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:
    [Visual Basic]
    If (myParameterValue.Kind = DiscreteOrRangeKind.DiscreteValue) Then
    
    End If
    [C#]
    if(parameterValue.Kind == DiscreteOrRangeKind.DiscreteValue)
    {
    }
  11. Within this nested conditional block, cast the ParameterValue instance to its extended class, DiscreteParameterValue.
    [Visual Basic]
    Dim myParameterDiscreteValue As ParameterDiscreteValue = CType(myParameterValue, ParameterDiscreteValue)
    [C#]
    ParameterDiscreteValue parameterDiscreteValue = (ParameterDiscreteValue)parameterValue;
  12. Also within the nested conditional block, add the Value property of the ParameterDiscreteValue instance (converted to String) into the ArrayList instance.
    [Visual Basic]
    myArrayList.Add(myParameterDiscreteValue.Value.ToString())
    [C#]
    arrayList.Add(parameterDiscreteValue.Value.ToString());
  13. Outside the conditional block, and outside the foreach loop, at the end of the method, return the ArrayList instance from the method.
    [Visual Basic]
    Return myArrayList
    [C#]
    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

  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.
    [Visual Basic]
    If Not IsPostBack Then
    
    End If
    [C#]
    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.
    [Visual Basic]
    defaultParameterValuesList.DataSource = GetDefaultValuesFromParameterField(customersByCityReport)
    [C#]
    defaultParameterValuesList.DataSource = GetDefaultValuesFromParameterField(customersByCityReport);
  4. Still within the Not IsPostBack conditional block, call the DataBind() method of the defaultParameterValuesList ListBox.
    [Visual Basic]
    defaultParameterValuesList.DataBind()
    [C#]
    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.
    [Visual Basic]
    defaultParameterValuesList.DataSource = GetDefaultValuesFromParameterField(customersByCityReport)
    [C#]
    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

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

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

Continue to Setting Parameters from ListBox Selections.

See Also

Tutorials: Reading and Setting Discrete Parameters | Tutorials and Sample Code | Tutorials' Sample Code Directory