日期:2014-05-17  浏览次数:21235 次

comboBox的值传到水晶报表打印,comboBox说找不到?
FORM1 

输入的值是comboBox1 【2012】 comboBox2 【1】 comboBox3 【7】

FORM2

这个是我的水晶报表打印代码,提示说comboBox不存在,是咋回事?

private void jzpprint_Load(object sender, EventArgs e)
        {
   
            string strg = Application.StartupPath.ToString();//得到应用程序路径
            strg = strg.Substring(0, strg.LastIndexOf("\\"));//截取路径信息
            strg = strg.Substring(0, strg.LastIndexOf("\\"));//截取路径信息
            strg += @"\report";//添加路径信息
            strg += @"\jzpreport.rpt";//添加文件名称
            myReport.Load(strg);
            ReportDocument myReport = new ReportDocument();
            myReport.SetParameterValue("参数1", comboBox1.Text);
            myReport.SetParameterValue("参数2", comboBox2.Text);
            myReport.SetParameterValue("参数3", comboBox3.Text);
            crystalReportViewer1.ReportSource = myReport;  

}
当前上下文中不存在名称“comboBox1” 错误 
当前上下文中不存在名称“comboBox2”错误 
当前上下文中不存在名称“comboBox3” 错误 

------解决方案--------------------
你在Form2中添加一个重载的构造函数参数是年月日,在show Form2的对象的时候传递过去
private int year;
private int month;
private int day;

Form2(int year,int month,int day)
{
    this.year = year;
    this.month = month;
    this.day = day;
}

myReport.SetParameterValue("参数1", comboBox1.Text);
             myReport.SetParameterValue("参数2", comboBox2.Text);
             myReport.SetParameterValue("参数3", comboBox3.Text);

改为
myReport.SetParameterValue("参数1", year);
myReport.SetParameterValue("参数2", month);
myReport.SetParameterValue("参数3", day);

显示Form2的代码

Form2 frm = new Form2(comboBox1.Text,comboBox2.Text,comboBox3.Text);
frm.show();