命名空间为:using CrystalDecisions.Shared
private void BuildReportViewerParameters(CrystalDecisions.Web.CrystalReportViewer rptViewer, Hashtable ParamTable)
{
//定义报表视图的参数的方法
ParameterFields ParamFields = new ParameterFields();
foreach (object key in ParamTable.Keys)
{
ParameterField ParamField = new ParameterField();
ParameterValues ParamValues = new ParameterValues();
ParameterDiscreteValue ParamValue = new ParameterDiscreteValue();
ParamValue.Value = ParamTable[key].ToString();
ParamValues.Add(ParamValue);
ParamField.ParameterFieldName = key.ToString();
ParamField.CurrentValues = ParamValues;
ParamFields.Add(ParamField);
}
rptViewer.ParameterFieldInfo = ParamFields;
}
private void BuildReportClassParameters(CrystalDecisions.CrystalReports.Engine.ReportClass rpt, Hashtable ParamTable)
{
//定义报表类的参数的方法
foreach (object key in ParamTable.Keys)
{
ParameterValues ParamValues = new ParameterValues();
ParameterDiscreteValue ParamValue = new ParameterDiscreteValue();
ParamValue.Value = ParamTable[key].ToString();
ParamValues.Add(ParamValue);
rpt.DataDefinition.ParameterFields[key.ToString()].ApplyCurrentValues(ParamValues);
}
}
protected void BuildReportParameters(CrystalDecisions.Web.CrystalReportViewer rpt,Hashtable ParamTable)
{
//定义报表参数的方法
ParameterFields ParamFields = new ParameterFields();
foreach(object key in ParamTable.Keys)
{
ParameterField ParamField;
ParameterValues ParamValues;
ParameterDiscreteValue ParamValue;
ParamField = new ParameterField();
ParamValues = new ParameterValues();
ParamValue = new ParameterDiscreteValue();
ParamValue.Value = (string)ParamTable[key];
ParamValues.Add(ParamValue);
ParamField.ParameterFieldName=(string)key;
ParamField.CurrentValues = ParamValues;
ParamFields.Add(ParamField);
}
rpt.ParameterFieldInfo = ParamFields;
}
然后再你的需要调用参数的地方,加如下引用:
private void Button_output_Click(object sender, System.EventArgs e)
{
CrystalDecisions.Shared.TableLogOnInfo logInfo = new CrystalDecisions.Shared.TableLogOnInfo(); //设置报表的登录信息
logInfo.ConnectionInfo.ServerName = "ANGEL"; //设置报表与库的连接信息
logInfo.ConnectionInfo.DatabaseName = "KaoQin";
logInfo.TableName = "Emstatistic";
logInfo.ConnectionInfo.UserID = "sa";
logInfo.ConnectionInfo.Password = "";
CrystalReportViewer1.ReportSource = Server.MapPath("cr_aa.rpt"); //指定报表的数据源
CrystalReportViewer1.LogOnInfo.Add(logInfo);//对参数进行赋什值
// CrystalReportViewer1.DataBind();
//声明变量并获取导出选项
cr_aa myReport = new cr_aa(); //定义一个名为myReport的报表
myReport.Database.Tables[0].ApplyLogOnInfo(logInfo);
CrystalReportViewer1.ReportSource = myReport;
CrystalReportViewer1.DataBind();
Hashtable t = new Hashtable();