Export (0) Print
Expand All

Parameter Fields Runtime Customization

Visual Studio .NET 2003

You can support user input with parameters in Crystal reports. Such parameters can be used for a variety of purposes. For example:

  • Base a parameter on a database field and allow the user to specify values for that field to filter data in the report.
  • Apply conditional formatting to a report by using parameter fields.
  • Define sort order by using parameter fields.

The following example demonstrates how parameter field values can be set, through code, at runtime. It explains how to set two different parameters. The first is a multiple-value discrete parameter, and the second is a range value parameter.

To Modify Parameter Fields at Runtime

[Visual Basic]
' Declare variables needed to pass the parameters
' to the viewer control.
Dim paramFields As New ParameterFields()
Dim paramField As New ParameterField()
Dim discreteVal As New ParameterDiscreteValue()
Dim rangeVal As New ParameterRangeValue()

' The first parameter is a discrete parameter with multiple values.

' Set the name of the parameter field, this must match a 
' parameter in the report.
paramField.ParameterFieldName = "Customer Name"

' Set the first discrete value and pass it to the parameter
discreteVal.Value = "AIC Childrens"
paramField.CurrentValues.Add(discreteVal)

' Set the second discrete value and pass it to the parameter.
' The discreteVal variable is set to new so the previous settings
' will not be overwritten.
discreteVal = New ParameterDiscreteValue()
discreteVal.Value = "Aruba Sport"
paramField.CurrentValues.Add(discreteVal)

' Add the parameter to the parameter fields collection.
paramFields.Add(paramField)

' The second parameter is a range value. The paramField variable
' is set to new so the previous settings will not be overwritten.
paramField = New ParameterField()

' Set the name of the parameter field, this must match a
' parameter in the report.
paramField.ParameterFieldName = "Customer ID"

' Set the start and end values of the range and pass it to the 'parameter.
rangeVal.StartValue = 42
rangeVal.EndValue = 72
paramField.CurrentValues.Add(rangeVal)

' Add the second parameter to the parameter fields collection.
paramFields.Add(paramField)

' Set the parameter fields collection into the viewer control.
crystalReportViewer1.ParameterFieldInfo = paramFields

crystalReportViewer1.ReportSource = "c:\reports\my report.rpt"
[C#]
// Declare variables needed to pass the parameters
// to the viewer control.
ParameterFields paramFields = new ParameterFields ();
ParameterField paramField = new ParameterField ();
ParameterDiscreteValue discreteVal = new ParameterDiscreteValue ();
ParameterRangeValue rangeVal = new ParameterRangeValue ();

// The first parameter is a discrete parameter with multiple values.

// Set the name of the parameter field, this must match a 
// parameter in the report.
paramField.ParameterFieldName = "Customer Name";

// Set the first discrete value and pass it to the parameter.
discreteVal.Value = "AIC Childrens";
paramField.CurrentValues.Add (discreteVal);

// Set the second discrete value and pass it to the parameter.
// The discreteVal variable is set to new so the previous settings
// will not be overwritten.
discreteVal = new ParameterDiscreteValue ();
discreteVal.Value = "Aruba Sport";
paramField.CurrentValues.Add (discreteVal);

// Add the parameter to the parameter fields collection.
paramFields.Add (paramField);

// The second parameter is a range value. The paramField variable
// is set to new so the previous settings will not be overwritten.
paramField = new ParameterField ();

// Set the name of the parameter field, this must match a
// parameter in the report.
paramField.ParameterFieldName = "Customer ID";

// Set the start and end values of the range and pass it to the 
// parameter.
rangeVal.StartValue = 42;
rangeVal.EndValue = 72;
paramField.CurrentValues.Add (rangeVal);

// Add the second parameter to the parameter fields collection.
paramFields.Add (paramField);

// Set the parameter fields collection into the viewer control.
crystalReportViewer1.ParameterFieldInfo = paramFields;
[C++]
// Declare variables needed to pass the parameters
// to the viewer control.
ParameterFields* paramFields = new ParameterFields ();
ParameterField* paramField = new ParameterField ();
ParameterDiscreteValue* discreteVal = new ParameterDiscreteValue ();
ParameterRangeValue* rangeVal = new ParameterRangeValue ();

// The first parameter is a discrete parameter with multiple values.
// Set the name of the parameter field, this must match a 
// parameter in the report.
paramField->ParameterFieldName = "Customer Name";

// Set the first discrete value and pass it to the parameter.
String* val = "AIC Childrens";
discreteVal->Value = val;
paramField->CurrentValues->Add (discreteVal);

// Set the second discrete value and pass it to the parameter.
// The discreteVal variable is set to new so the previous settings
// will not be overwritten.
discreteVal = new ParameterDiscreteValue ();
val = "Aruba Sport";
discreteVal->Value = val;
paramField->CurrentValues->Add (discreteVal);

// Add the parameter to the parameter fields collection.
paramFields->Add (paramField);

// The second parameter is a range value. The paramField variable
// is set to new so the previous settings will not be overwritten.
paramField = new ParameterField ();

// Set the name of the parameter field, this must match a
// parameter in the report.
paramField->ParameterFieldName = "Customer ID";

// Set the start and end values of the range and pass it to the 
// parameter.
rangeVal->StartValue = __box(42);
rangeVal->EndValue = __box(72);
paramField->CurrentValues->Add (rangeVal);

// Add the second parameter to the parameter fields collection.
paramFields->Add (paramField);

// Set the parameter fields collection into the viewer control.
crystalReportViewer1->ParameterFieldInfo = paramFields;

crystalReportViewer1->ExportReport();
[VJ#]
// Declare variables needed to pass the parameters
// to the viewer control.
ParameterFields paramFields = new ParameterFields ();
ParameterField paramField = new ParameterField ();
ParameterDiscreteValue discreteVal = new ParameterDiscreteValue ();
ParameterRangeValue rangeVal = new ParameterRangeValue ();

// The first parameter is a discrete parameter with multiple values.

// Set the name of the parameter field, this must match a 
// parameter in the report.
paramField.set_ParameterFieldName("Customer Name");

// Set the first discrete value and pass it to the parameter.
discreteVal.set_Value("AIC Childrens");
paramField.get_CurrentValues.Add (discreteVal);

// Set the second discrete value and pass it to the parameter.
// The discreteVal variable is set to new so the previous settings
// will not be overwritten.
discreteVal = new ParameterDiscreteValue ();
discreteVal.set_Value("Aruba Sport");
paramField.get_CurrentValues().Add (discreteVal);

// Add the parameter to the parameter fields collection.
paramFields.Add (paramField);

// The second parameter is a range value. The paramField variable
// is set to new so the previous settings will not be overwritten.
paramField = new ParameterField ();

// Set the name of the parameter field, this must match a
// parameter in the report.
paramField.set_ParameterFieldName("Customer ID");

// Set the start and end values of the range and pass it to the 
// parameter.
rangeVal.set_StartValue((System.Int32)42);
rangeVal.set_EndValue((System.Int32)72);
paramField.get_CurrentValues().Add (rangeVal);

// Add the second parameter to the parameter fields collection.
paramFields.Add (paramField);

// Set the parameter fields collection into the viewer control.
crystalReportViewer1.set_ParameterFieldInfo(paramFields);

See Also

Setting Parameters | Binding Options for Windows Forms Viewers | Binding Options for Web Forms Viewers

Show:
© 2014 Microsoft