Dialog box appear for parameter Field in Crystal Report

603 Views Asked by At

I want to pass a parameter from visual studion WinForm to crystalReport I have done this:

rptDoc.SetDataSource(dt);           
ParameterFields paramFields = new ParameterFields();
ParameterField paramField = new ParameterField();
ParameterDiscreteValue paramDiscreteValue = new ParameterDiscreteValue();
paramField = new ParameterField();
paramDiscreteValue = new ParameterDiscreteValue();
paramField.Name = "CableRevision";
paramDiscreteValue.Value = currentInfo.CableRevision;
paramField.CurrentValues.Add(paramDiscreteValue);
paramFields.Add(paramField);

crystalReportViewer1.ReportSource = rptDoc;

and also i have defined a Parameter Field of Field Explorer in Report Named "@CableRevision". should it be Dynamic or Static? I have tried both but every time a dialog box and asks for CableRevision Parametere Value. i dont wan't that dialog box get the value because i want to pass the value by

paramDiscreteValue.Value = currentInfo.CableRevision;

is that wrong?

1

There are 1 best solutions below

0
On

Your paramField didn't connect to rptDoc yet.

You may try this code instead

ParameterFieldDefinitions crParams = rptDoc.DataDefinition.ParameterFields;
foreach (ParameterFieldDefinition def in crParams)
{
   if (def.IsLinked())
   {
      continue;
   }

   // create new Parameter Discrete Value object 
   ParameterDiscreteValue paramDiscreteValue = new ParameterDiscreteValue();

   string paramName = def.ParameterFieldName;
   switch (paramName)
   {
      case "@CableRevision": paramDiscreteValue.Value = currentInfo.CableRevision; break;
      case "@OtherParam": paramDiscreteValue.Value = OtherParamValue; break;
   }

   // extract collection of current values 
   ParameterValues currentValues = def.CurrentValues;

   // Add Discrete value object to collection of current values 
   currentValues.Add(paramDiscreteValue);

   // apply modified current values to param collection 
   def.ApplyCurrentValues(currentValues);
}

rptDoc.DataDefinition.ParameterFields will return all parameters in the rptDoc. Then you have to set value for each parameter as in switch scope.

This is sample for many patameters in report, if you have only on parameter, you don't need to have switch scope, just have this line instead

paramDiscreteValue.Value = currentInfo.CableRevision;