Right-click the project name, and click Set As Startup Project. We're now ready to start working with parameters.

The sample report that goes along with this section is called ch4_parameters.rpt and to add this report to your project, select Project ^ Add New Item and browse for the report where you unzipped the downloaded sample files. Add it to the project and click the Open button.

Now drag and drop a CrystalReportViewer onto your form and leave room at the top for some additional fields we are going to add a little later.

You will also need to add this report as a component to your form. Switch to the Layout view of your form and look in the toolbox under Components. In this section, you should see a component labeled ReportDocument. Drag this component onto your form.

From the drop-down list, select the ch4_parameters.rpt report and click OK. We are now ready to look at how to set the parameters for this report.

To start, think back to our discussion of the different types of parameters you could have in a report—in addition to a parameter's data type (String, Number, and so on), you can also set options for how the parameter is entered — there are three basic options:

□ Discrete parameters accept single, discrete values — depending on how the parameter field was set up, it can accept only one value or multiple values.

□ Ranged parameters can accept a lower and upper value, selecting everything that is within that range of numbers, letters, and so on.

□ Discrete and Ranged parameters support a combination of the two different types, where you can enter multiple discrete values as well as multiple ranges.

Are you confused now? It can get a bit tricky when you create a report that can accept a combination of the different types, but we are going to walk through these scenarios so you know how to use them with your own reports and parameters.

We are going to start by looking at the easiest implementation—a single, discrete parameter with only one value to enter. Parameters within a report are contained within a collection called ParameterFields and within the collection, a set of attributes is stored for each field using the Parameter Field object. As we walk through this example, we will set properties relating to this object in this collection and then use this collection to set the ParameterFieldInfo property of the viewer, as shown here:

crystalReportViewer1.ParameterFieldInfo = myParameterFields

To set the values for a particular parameter, we need to first dimension both the collection and object. We also need to create a variable to hold the value that we want to pass to the report for this parameter.

Because in this example we are only passing a discrete value to the parameter, we would simply create a variable by dimensioning it as a ParameterDiscreteValue. All we need to do at that point is set the value of the parameter equal to some value we have entered or hard-coded and then set the ParameterFieldInfo property of the Report Viewer.

Because we want to enter the value ourselves, we would want to put a textbox and a command button on our form to collect and submit the value to be used in our parameter, which would make the form look something like Figure 4-15.

Figure 4-15

To change your form, draw a textbox and a command button on your form, which will be named TextBoxl and Commandl by default. Set the properties of the textbox to make it blank when the form appears and change the name of the command button to "View Report," and then double-click the button on your form to open the code view. This is where we are going to put our completed code that we just walked through. It should look something like this:

Dim myParameterFields As New ParameterFields()

Dim myParameterField As New ParameterField()

Dim myDiscreteValue As New ParameterDiscreteValue()

myParameterField.ParameterFieldName = "OriginCountry" myDiscreteValue.Value = TextBoxl.Text myParameterField.CurrentValues.Add(myDiscreteValue)


crystalReportViewerl.ParameterFieldlnfo = myParameterFields crystalReportViewerl.refresh

The next scenario we are going to look at is for multiple discrete variables; in this situation, we would use the same methods, only instead of setting a single value using the ParameterDiscreteValue variable, we would continually redeclare this variable and use it to push values into the ParameterField object, as shown here:

Dim myParameterFields As New ParameterFields()

Dim myParameterField As New ParameterField()

Dim myDiscreteValue As New ParameterDiscreteValue()

myParameterField.ParameterFieldName = "OriginCountry"

myDiscreteValue.Value = "Australia" myParameterField.CurrentValues.Add(myDiscreteValue)

myDiscreteValue= New ParameterDiscreteValue()

myDiscreteValue.Value = "New Zealand" myParameterField.CurrentValues.Add(myDiscreteValue)


crystalReportViewerl.ParameterFieldlnfo = myParameterFields

In this example, we have used the variable twice to push both "Australia" and "New Zealand" to the report. Remember, in your actual report design, you must specify whether the parameter option is a discrete, multi-value discrete, ranged, and so on. Otherwise you will receive an error message when running your report.

Finally, our last type of parameter is a ranged parameter, which can be set using the same method, except instead of a discrete value, we are going to pass in a range, with a start and end value, as shown here:

Dim myParameterFields As New ParameterFields() Dim myParameterField As New ParameterField() Dim myDiscreteValue As New ParameterDiscreteValue() Dim myRangeValues as New ParamterRangeValues()

myParameterField = New ParameterField() myParameterField.ParameterFieldName = "LastYearsSales"

myRangeValues.StartValue = 100000 myRangeValues.EndValue = 500000

myParameterField.CurrentValues.Add(myRangeValue) myParameterFields.Add(myParameterField)

You can also combine the two methods depending on the type and number of parameters you have created in your report. You could have two parameters, for example, that accepted discrete values and one that accepted a range and still yet another that accepted a combination of the two. The methods used to set the values for these parameters are the same, so users can easily enter parameter values through their own application and filter the report content.

Speaking of different types of users, the next section deals with how to customize the appearance and layout of the viewer itself. So in addition to showing them only the records they want to see, you could also give them their own custom viewer with which to view the resulting report.

Was this article helpful?

0 0

Post a comment