急切请教asp.net调用显示报表(包括输入参数和子报表)的问题

OKILOVE 2008-10-16 11:22:49
想用ReportDocument对象来实现加载报表,因为想自己设置一个输入参数的用户界面(不想用DataSource那个输入参数的界面)。
实例化ReportDocument rpd,装载报表,给报表连接数据库,用ArrayList装进参数,传到rpd中,最后父报表能正确显示。因为没有做过含有子报表的例子,本来觉得子报表的参数是和父报表的参数相联系的,只用传父报表参数即可,然而子报表不能显示。
因此给子报表连接数据库,再相同方法实现给子报表也传入参数,但是调试没语法错误后,报表中的子报表还是无法显示数据。
到底该怎么实现读取子报表?
恳请各位大虾指点!
...全文
173 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
阿泰 2008-10-16
  • 打赏
  • 举报
回复
C#版本标准调用方法:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

public partial class _Default : System.Web.UI.Page
{
private ReportDocument northwindCustomersReport;

private void ConfigureCrystalReports()
{
northwindCustomersReport = new ReportDocument();
string reportPath = Server.MapPath("NorthwindCustomers.rpt");
northwindCustomersReport.Load(reportPath);

ConnectionInfo connectionInfo = new ConnectionInfo();
connectionInfo.ServerName = "ENXP-50701E";
connectionInfo.DatabaseName = "Northwind";
connectionInfo.IntegratedSecurity = true;
SetDBLogonForReport(connectionInfo, northwindCustomersReport);

SetDBLogonForSubreports(connectionInfo, northwindCustomersReport);

crystalReportViewer.ReportSource = northwindCustomersReport;
}

private void Page_Init(object sender, EventArgs e)
{
ConfigureCrystalReports();
}

private void SetDBLogonForReport(ConnectionInfo connectionInfo, ReportDocument reportDocument)
{
Tables tables = reportDocument.Database.Tables;
foreach (CrystalDecisions.CrystalReports.Engine.Table table in tables)
{
TableLogOnInfo tableLogonInfo = table.LogOnInfo;
tableLogonInfo.ConnectionInfo = connectionInfo;
table.ApplyLogOnInfo(tableLogonInfo);
}
}

private void SetDBLogonForSubreports(ConnectionInfo connectionInfo, ReportDocument reportDocument)
{
Sections sections = reportDocument.ReportDefinition.Sections;
foreach (Section section in sections)
{
ReportObjects reportObjects = section.ReportObjects;
foreach (ReportObject reportObject in reportObjects)
{
if (reportObject.Kind == ReportObjectKind.SubreportObject)
{
SubreportObject subreportObject = (SubreportObject)reportObject;
ReportDocument subReportDocument = subreportObject.OpenSubreport(subreportObject.SubreportName);
SetDBLogonForReport(connectionInfo, subReportDocument);
}
}
}
}
}

mjjzg 2008-10-16
  • 打赏
  • 举报
回复
对图表研究甚少,帮你顶下
OKILOVE 2008-10-16
  • 打赏
  • 举报
回复
谢谢4楼,但是你这个是msdn上向报表传参数的例子吧,我就是模仿这个写的,
我的主报表已经能够传入参数显示正确了,
但是子报表不能正常显示,我用同样的方法向子报表传入参数,
还是不能正常显示,所以才猜想是不是不能用这样的方法。。
阿泰 2008-10-16
  • 打赏
  • 举报
回复
带参数子报表标准操作方法:

using System;
using System.Data;
using System.Collections;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;


public partial class _Default : System.Web.UI.Page
{
private ReportDocument customersByCityReport;
private const string PARAMETER_FIELD_NAME = "City";
private const string SUBREPORT_PARAMETER_FIELD_NAME = "OrderDateRange";
private const string SUBREPORT_NAME = "CustomerOrders";


protected void Page_Load(object sender, EventArgs e)
{

}

private void Page_Init(object sender, EventArgs e)
{
ConfigureCrystalReports();
}

private void ConfigureCrystalReports()
{
customersByCityReport = new ReportDocument();
string reportPath = Server.MapPath("CustomersByCity.rpt");
customersByCityReport.Load(reportPath);

ArrayList arrayList = new ArrayList();

string startDate;
string endDate;

if (!IsPostBack)
{
defaultParameterValuesList.DataSource = GetDefaultValuesFromParameterField(customersByCityReport);
defaultParameterValuesList.DataBind();
arrayList.Add("Paris");
arrayList.Add("Tokyo");
startDate = "8/1/1997";
endDate = "8/31/1997";

Session["arrayList"] = arrayList;
Session["startDate"] = startDate;
Session["endDate"] = endDate;
}

else
{
arrayList = (ArrayList)Session["arrayList"];
startDate = Session["startDate"].ToString();
endDate = Session["endDate"].ToString();
}

SetCurrentValuesForParameterField(customersByCityReport, arrayList);

SetDateRangeForOrders(customersByCityReport, startDate, endDate);

crystalReportViewer.ReportSource = customersByCityReport;
}


private void SetCurrentValuesForParameterField(ReportDocument reportDocument, ArrayList arrayList)
{
ParameterValues currentParameterValues = new ParameterValues();
foreach (object submittedValue in arrayList)
{
ParameterDiscreteValue parameterDiscreteValue = new ParameterDiscreteValue();
parameterDiscreteValue.Value = submittedValue.ToString();
currentParameterValues.Add(parameterDiscreteValue);
}

ParameterFieldDefinitions parameterFieldDefinitions = reportDocument.DataDefinition.ParameterFields;
ParameterFieldDefinition parameterFieldDefinition = parameterFieldDefinitions[PARAMETER_FIELD_NAME];
parameterFieldDefinition.ApplyCurrentValues(currentParameterValues);

}

private ArrayList GetDefaultValuesFromParameterField(ReportDocument reportDocument)
{
ParameterFieldDefinitions parameterFieldDefinitions = reportDocument.DataDefinition.ParameterFields;
ParameterFieldDefinition parameterFieldDefinition = parameterFieldDefinitions[PARAMETER_FIELD_NAME];
ParameterValues defaultParameterValues = parameterFieldDefinition.DefaultValues;
ArrayList arrayList = new ArrayList();
foreach (ParameterValue parameterValue in defaultParameterValues)
{
if (!parameterValue.IsRange)
{
ParameterDiscreteValue parameterDiscreteValue = (ParameterDiscreteValue)parameterValue;
arrayList.Add(parameterDiscreteValue.Value.ToString());
}

}
return arrayList;
}

private void SetDateRangeForOrders(ReportDocument reportDocument, string startDate, string endDate)
{
ParameterRangeValue parameterRangeValue = new ParameterRangeValue();
parameterRangeValue.StartValue = startDate;
parameterRangeValue.EndValue = endDate;
parameterRangeValue.LowerBoundType = RangeBoundType.BoundInclusive;
parameterRangeValue.UpperBoundType = RangeBoundType.BoundInclusive;
ParameterFieldDefinitions parameterFieldDefinitions = reportDocument.DataDefinition.ParameterFields;
ParameterFieldDefinition parameterFieldDefinition = parameterFieldDefinitions[SUBREPORT_PARAMETER_FIELD_NAME, SUBREPORT_NAME];
parameterFieldDefinition.CurrentValues.Clear();
parameterFieldDefinition.CurrentValues.Add(parameterRangeValue);
parameterFieldDefinition.ApplyCurrentValues(parameterFieldDefinition.CurrentValues);
}


protected void redisplay_Click(object sender, EventArgs e)
{
ArrayList arrayList = new ArrayList();
foreach (ListItem item in defaultParameterValuesList.Items)
{
if (item.Selected)
{
arrayList.Add(item.Value);
}
}
Session["arrayList"] = arrayList;

Session["startDate"] = orderStartDate.Text;
Session["endDate"] = orderEndDate.Text;

ConfigureCrystalReports();
}
}

NIJIA72 2008-10-16
  • 打赏
  • 举报
回复
帮你顶一下
OKILOVE 2008-10-16
  • 打赏
  • 举报
回复
我就是模仿1楼说的方法来做的,但是我的子报表含有输入参数,因此产生问题了,还希望请教对于有输入参数的子报表
的显示方法。

4,818

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 图表区
社区管理员
  • 图表区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧