//Create a new instance of a discrete parameter object to set the //创建并初始化参数的第一个离散值 //first value for the parameter crParameterDiscreteValue = new ParameterDiscreteValue(); crParameterDiscreteValue.Value = "Canada"; //Define the parameter field to pass the parameter values to //定义参数字段以传递参数值 //注意,参数的名称必须和放入报表中的参数字段的名称一致 crParameterField = new ParameterField(); crParameterField.ParameterFieldName = "Country"; //Pass the first value to the discrete parameter //传递第一个离散值给参数 crParameterField.CurrentValues.Add(crParameterDiscreteValue); //Destroy the current instance of the discrete value //释放当前参数离散值实例 crParameterDiscreteValue = null; //Create a new instance of discrete value object to set the second //新建并初始化参数的另一个离散值 //value for the parameter crParameterDiscreteValue = new ParameterDiscreteValue(); crParameterDiscreteValue.Value = "USA"; //Pass the second value to the discrete parameter //将其传递给参数 crParameterField.CurrentValues.Add(crParameterDiscreteValue); //Create an instance of the parameter fields collection, and //创建参数字段集合 //pass the discrete parameter with the two discrete values to the //并把这个离散参数连并它的离散值一起传递给参数集合 //collection of parameter fields crParameterFields = new ParameterFields(); crParameterFields.Add(crParameterField); //The collection of parameter fields must be set to the viewer //然后必须把这个参数集合设置为当前查看器的参数集合 crystalReportViewer1.ParameterFieldInfo = crParameterFields; //Set the viewer to the report object to be previewed //This must be done after the parameter information has been set //在设置完参数后,才能将报表设置给报表查看器 crystalReportViewer1.ReportSource = crReportDocument;
不过这种方法在.net有时候参数字段会丢失,没有办法。另一种方法将你需要的信息,比如起始年月,终止年月都放在dataset中,在cr中直接将字段拖上去就行了。
其实不用传变量,可以换个方法,只传记录集给报表即可,但你的水晶报表显示结构是确定的...
看这个能让你明白一点不:
http://community.csdn.net/Expert/topic/3173/3173643.xml?temp=7.697695E-02
//创建并初始化参数的第一个离散值
//first value for the parameter
crParameterDiscreteValue = new ParameterDiscreteValue();
crParameterDiscreteValue.Value = "Canada"; //Define the parameter field to pass the parameter values to
//定义参数字段以传递参数值
//注意,参数的名称必须和放入报表中的参数字段的名称一致
crParameterField = new ParameterField();
crParameterField.ParameterFieldName = "Country";
//Pass the first value to the discrete parameter
//传递第一个离散值给参数
crParameterField.CurrentValues.Add(crParameterDiscreteValue);
//Destroy the current instance of the discrete value
//释放当前参数离散值实例
crParameterDiscreteValue = null;
//Create a new instance of discrete value object to set the second
//新建并初始化参数的另一个离散值
//value for the parameter
crParameterDiscreteValue = new ParameterDiscreteValue();
crParameterDiscreteValue.Value = "USA"; //Pass the second value to the discrete parameter
//将其传递给参数
crParameterField.CurrentValues.Add(crParameterDiscreteValue); //Create an instance of the parameter fields collection, and
//创建参数字段集合
//pass the discrete parameter with the two discrete values to the
//并把这个离散参数连并它的离散值一起传递给参数集合
//collection of parameter fields
crParameterFields = new ParameterFields();
crParameterFields.Add(crParameterField); //The collection of parameter fields must be set to the viewer
//然后必须把这个参数集合设置为当前查看器的参数集合
crystalReportViewer1.ParameterFieldInfo = crParameterFields; //Set the viewer to the report object to be previewed
//This must be done after the parameter information has been set
//在设置完参数后,才能将报表设置给报表查看器
crystalReportViewer1.ReportSource = crReportDocument;