Programming the CrystalReportViewer Control
Programming with the viewer has some similarities to programming with the report document, but there differences. From a high level, the viewer is similar to the report object because you have to get a reference to the parameter object and assign a value to its CurrentValues collection. From an implementation standpoint, the viewer is different because it uses different parameter classes. You’ll find it easier to program parameters using the viewer control than using the report document.
The viewer uses the ParameterFields collection to manage the properties of each parameter in a report. The ParameterFields collection manages the ParameterField object. It is easier to work with the ParameterField object (compared to the ParameterFieldDefinition object of the report document) because the ParameterField object lets you reference the CurrentValues collection directly. This makes more sense conceptually and it saves you some coding.
The viewer also requires you to assign the report object to the ReportSource property first. The viewer examines the report object and builds a list of parameters in the report. It has to build this list before you can reference the individual parameter objects. The last step is to assign a value to the parameter object’s CurrentValues collection.
Programming with the CrystalReportViewer consists of 8 steps:
Listing 16-3 illustrates these steps. It creates a parameter that is a range value and assigns it to the CurrentValues collection. The in-line comments explain the steps that are taking place.
Dim ParameterFields As CrystalDecisions.Shared.ParameterFields
Dim ParameterField As CrystalDecisions.Shared.ParameterField
Dim ParameterRangeValue As CrystalDecisions.Shared.ParameterRangeValue
'Step 1: Assign the report object to the viewer
Dim MyReport As New CrystalReport1
CrystalReportViewer1.ReportSource = MyReport
'Step 2: Reference the ParameterFields collection
ParameterFields = CrystalReportViewer1.ParameterFieldInfo
'Step 3: Reference the ParameterField object
ParameterField = ParameterFields("DateRange")
'Step 4: Create a ParameterValue object.
'This example uses a RangeValue object
ParameterRangeValue = New CrystalDecisions.Shared.ParameterRangeValue
'Step 5: Assign a value to the object
ParameterRangeValue.StartValue = "1/4/1997"
ParameterRangeValue.EndValue = "2/2/1997"
'Step 6: Add the ParameterValue object to either the
'CurrentValues or DefaultValues collection
ParameterField.CurrentValues.Add(ParameterRangeValue)