using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
/// <summary>
/// 绑定显示报表
/// </summary>
private void BindReport()
{
ReportDocument report = new ReportDocument();
//加载报表
report.Load(Server.MapPath("./Report/xx.rpt"));
//绑定报表数据源(crv:CrystalReportViewer报表预览控件名)
report.SetDataSource(datatable); //设置报表数据源
crv.ReportSource = report; //绑定报表文档到预览控件
crv.ParameterFieldInfo = this.GetReportParameters(); //参数信息
crv.PageZoomFactor = 100; //缩放100
crv.DataBind(); //绑定报表显示
}
/// <summary>
/// 获得报表参数信息
/// </summary>
private ParameterFields GetParameterFields()
{
ParameterFields fields = new ParameterFields();
//单位、部门、人员、日期范围参数
fields.Add(this.CreateParameterField("@orgname", "单位"));
fields.Add(this.CreateParameterField("@empname", "人员"));
fields.Add(this.CreateParameterField("@daterange", "日期"));
//返回
return fields;
}
/// <summary>
/// 建立参数字段
/// </summary>
/// <param name="FieldName">字段名</param>
/// <param name="FieldValue">字段值</param>
/// <returns>参数字段</returns>
private ParameterField CreateParameterField(string FieldName, object FieldValue)
{
ParameterField field = new ParameterField();
ParameterDiscreteValue pvalue = new ParameterDiscreteValue();
pvalue.Value = FieldValue;
field.Name = FieldName;
field.CurrentValues.Add(pvalue);
//返回参数字段
return field;
}
来源:https://www.cnblogs.com/micromouse/archive/2008/02/20/1075002.html