C# Code Listings
The C# code listings are equivalent to the VB.NET code listings.
CrystalReport1 MyReport = new CrystalReport1();
MapParameterFieldDefinitions(MyReport.DataDefinition.ParameterFields, 0);
…
public void MapParameterFieldDefinitions(
CrystalDecisions.CrystalReports.Engine.ParameterFieldDefinitions
ParameterFieldDefinitions, int Indent)
{
foreach(CrystalDecisions.CrystalReports.Engine.ParameterFieldDefinition
ParameterFieldDefinition in ParameterFieldDefinitions)
{
MapParameterFieldDefinition(ParameterFieldDefinition, Indent);
}
}
public void MapParameterFieldDefinition(
CrystalDecisions.CrystalReports.Engine.ParameterFieldDefinition
ParameterFieldDefinition, int Indent)
{
Output("Name: " + ParameterFieldDefinition.ParameterFieldName, Indent);
Output("PromptText: " + ParameterFieldDefinition.PromptText, Indent + 2);
Output("ValueType: " +
ParameterFieldDefinition.ParameterValueKind.ToString(), Indent + 2);
Output("Kind: " +
ParameterFieldDefinition.DiscreteOrRangeKind.ToString(), Indent + 2);
if (ParameterFieldDefinition.CurrentValues.Count>0)
{
Output("Current Values: ", Indent + 2);
MapParameterValues(ParameterFieldDefinition.CurrentValues, Indent + 4);
}
if (ParameterFieldDefinition.DefaultValues.Count>0)
{
Output("Default Values", Indent + 2);
MapParameterValues(ParameterFieldDefinition.DefaultValues, Indent + 4);
}
}
public void MapParameterValues(CrystalDecisions.Shared.ParameterValues ParameterValues, int Indent)
{
foreach(CrystalDecisions.Shared.ParameterValue ParameterValue in
ParameterValues)
{
if (ParameterValue is CrystalDecisions.Shared.ParameterRangeValue)
{
MapParameterRangeValue((CrystalDecisions.Shared.ParameterRangeValue)
ParameterValue, Indent);
}
else
{
MapParameterDiscreteValue(
(CrystalDecisions.Shared.ParameterDiscreteValue)ParameterValue,
Indent);
}
}
}
public void MapParameterDiscreteValue(
CrystalDecisions.Shared.ParameterDiscreteValue ParameterValue, int Indent)
{
Output("Value: " + ParameterValue.Value.ToString(), Indent);
}
public void MapParameterRangeValue(
CrystalDecisions.Shared.ParameterRangeValue ParameterValue, int Indent)
{
Output("Value: " + ParameterValue.StartValue.ToString() + " to " + ParameterValue.EndValue.ToString(), Indent);
}
CrystalDecisions.Shared.ParameterDiscreteValue ParameterDiscreteValue;
CrystalDecisions.Shared.ParameterValues ParameterValues;
//Use the ReportDocument object to create two default parameters
CrystalReport1 MyReport = new CrystalReport1();
CrystalDecisions.CrystalReports.Engine.ParameterFieldDefinition ParameterFieldDefinition;
//Step 1: Get a reference to the property field to modify
ParameterFieldDefinition = MyReport.DataDefinition.ParameterFields["Location"];
//Step 2: Instantiate a ParameterValues collection object
ParameterValues = new CrystalDecisions.Shared.ParameterValues();
//Step 3: Instantiate a ParameterValue object
//This example uses a discrete value
ParameterDiscreteValue = new CrystalDecisions.Shared.ParameterDiscreteValue();
//Step 4: Assign a value to the object
ParameterDiscreteValue.Value = "Louisville, KY";
//Step 5: Add it to the ParameterValues collection using the Add() method
ParameterValues.Add(ParameterDiscreteValue);
//Step 6: Repeat steps 3-5 for additional parameters
ParameterDiscreteValue = new CrystalDecisions.Shared.ParameterDiscreteValue();
ParameterDiscreteValue.Value = "San Diego, CA";
ParameterValues.Add(ParameterDiscreteValue);
//Step 7: Assign the ParameterValues collection to the parameter field
//Do this for either the CurrentValues or DefaultValues collection
ParameterFieldDefinition.ApplyDefaultValues(ParameterValues);
//Step 8: Print or Preview the report
crystalReportViewer1.ReportSource = MyReport;
CrystalDecisions.Shared.ParameterFields ParameterFields;
CrystalDecisions.Shared.IParameterField ParameterField;
CrystalDecisions.Shared.ParameterRangeValue ParameterRangeValue;
//Step 1: Assign the report object to the viewer
CrystalReport1 MyReport = new CrystalReport1();
crystalReportViewer1.ReportSource = MyReport;
//Step 2: Reference the ParameterFields collection
ParameterFields = crystalReportViewer1.ParameterFieldInfo;
//Step 3: Reference the ParameterField
ParameterField = ParameterFields["DateRange"];
//Step 4: Create a ParameterValue object
//This example uses a range value object.
//You could also use a discrete value object.
ParameterRangeValue = new CrystalDecisions.Shared.ParameterRangeValue();
//Step 5: Assign a value to the object
ParameterRangeValue.StartValue = "1/4/1997";
ParameterRangeValue.EndValue = "1/20/1997";
//Step 6: Add the ParameterValue object to either the
//CurrentValues or DefaultValues collection
ParameterField.CurrentValues.Add(ParameterRangeValue);
public void SetParameterDiscreteWithReport(
CrystalDecisions.CrystalReports.Engine.ReportDocument MyReport,
string ParameterName, object ParameterValue, string SubreportName)
{
CrystalDecisions.CrystalReports.Engine.ParameterFieldDefinition
ParameterFieldDefinition;
CrystalDecisions.Shared.ParameterDiscreteValue ParameterDiscreteValue;
CrystalDecisions.Shared.ParameterValues ParameterValues;
ParameterFieldDefinition =
MyReport.DataDefinition.ParameterFields[ParameterName, SubreportName];
ParameterValues = new CrystalDecisions.Shared.ParameterValues();
ParameterDiscreteValue =
new CrystalDecisions.Shared.ParameterDiscreteValue();
ParameterDiscreteValue.Value = ParameterValue;
ParameterValues.Add(ParameterDiscreteValue);
ParameterFieldDefinition.ApplyCurrentValues(ParameterValues);
}
public void SetParameterRangeWithReport(
CrystalDecisions.CrystalReports.Engine.ReportDocument MyReport,
string ParameterName, object StartValue, object EndValue, CrystalDecisions.Shared.RangeBoundType LowerBoundType, CrystalDecisions.Shared.RangeBoundType UpperBoundType, string SubreportName)
{
CrystalDecisions.CrystalReports.Engine.ParameterFieldDefinition
ParameterFieldDefinition;
CrystalDecisions.Shared.ParameterRangeValue ParameterRangeValue;
CrystalDecisions.Shared.ParameterValues ParameterValues;
ParameterFieldDefinition = MyReport.DataDefinition.ParameterFields
[ParameterName, SubreportName];
ParameterValues = new CrystalDecisions.Shared.ParameterValues();
ParameterRangeValue = new CrystalDecisions.Shared.ParameterRangeValue();
ParameterRangeValue.StartValue = StartValue;
ParameterRangeValue.EndValue = EndValue;
ParameterRangeValue.LowerBoundType = LowerBoundType;
ParameterRangeValue.UpperBoundType = UpperBoundType;
ParameterValues.Add(ParameterRangeValue);
ParameterFieldDefinition.ApplyCurrentValues(ParameterValues);
}
public void SetParameterDiscreteWithViewer(
CrystalDecisions.Windows.Forms.CrystalReportViewer crystalReportViewer1,
string ParameterName, object ParameterValue, string SubreportName)
{
CrystalDecisions.Shared.IParameterField ParameterField;
CrystalDecisions.Shared.ParameterDiscreteValue ParameterDiscreteValue;
ParameterField = crystalReportViewer1.ParameterFieldInfo[ParameterName,
SubreportName];
ParameterDiscreteValue =
New CrystalDecisions.Shared.ParameterDiscreteValue();
ParameterDiscreteValue.Value = ParameterValue;
ParameterField.CurrentValues.Add(ParameterDiscreteValue);
}
public void SetParameterRangeWithViewer(
CrystalDecisions.Windows.Forms.CrystalReportViewer crystalReportViewer1,
string ParameterName, object StartValue, object EndValue, CrystalDecisions.Shared.RangeBoundType LowerBoundType, CrystalDecisions.Shared.RangeBoundType UpperBoundType, string SubreportName)
{
CrystalDecisions.Shared.IParameterField ParameterField;
CrystalDecisions.Shared.ParameterRangeValue ParameterRangeValue;
ParameterField = crystalReportViewer1.ParameterFieldInfo
[ParameterName, SubreportName];
ParameterRangeValue = new CrystalDecisions.Shared.ParameterRangeValue();
ParameterRangeValue.StartValue = StartValue;
ParameterRangeValue.EndValue = EndValue;
ParameterRangeValue.LowerBoundType = LowerBoundType;
ParameterRangeValue.UpperBoundType = UpperBoundType;
ParameterField.CurrentValues.Add(ParameterRangeValue);
}
CrystalReport1 MyReport = new CrystalReport1();
SetParameterDiscreteWithReport(MyReport, "MyBirthday", new DateTime(1968,5,23), "");
SetParameterRangeWithReport(MyReport, "DateRange",new DateTime(1997,2,1), new DateTime(1997,3,1), CrystalDecisions.Shared.RangeBoundType.BoundInclusive, CrystalDecisions.Shared.RangeBoundType.BoundExclusive, "");
crystalReportViewer1.ReportSource = MyReport;
CrystalReport1 MyReport = new CrystalReport1();
crystalReportViewer1.ReportSource = MyReport;
SetParameterDiscreteWithViewer(crystalReportViewer1, "CustomerId",
"DotNet Tech", "");
SetParameterRangeWithViewer(crystalReportViewer1, "DateRange",
new DateTime(1997, 1, 1),new DateTime(1997, 2, 1), CrystalDecisions.Shared.RangeBoundType.BoundInclusive, CrystalDecisions.Shared.RangeBoundType.BoundExclusive, "");