当前位置:编程学习 > C#/ASP.NET >>

C#怎么把水晶报表显示在crystalReportViewer控件中

要具体过程,比如数据库的DATASET什么的都怎么写。一步步把代码写上就行。报表已经创建好了。我的主要目的是要在这个控件中显示不同的报表。谢谢
答案:customerReport = new ReportDocument();
string reportPath = Server.MapPath("PrintGif\\CrystalReport.rpt");
customerReport.Load(reportPath);
customerReport.SetDataSource(dt);

ParameterFields paramFields = new ParameterFields();
ParameterField paramField1 = new ParameterField();
ParameterField paramField2 = new ParameterField();
ParameterField paramField3 = new ParameterField();
ParameterField paramField4 = new ParameterField();
ParameterField paramField5 = new ParameterField();
ParameterField paramField6 = new ParameterField();
ParameterField paramField7 = new ParameterField();
ParameterField paramField8 = new ParameterField();
ParameterDiscreteValue discreteVal1 = new ParameterDiscreteValue();
ParameterDiscreteValue discreteVal2 = new ParameterDiscreteValue();
ParameterDiscreteValue discreteVal3 = new ParameterDiscreteValue();
ParameterDiscreteValue discreteVal4 = new ParameterDiscreteValue();
ParameterDiscreteValue discreteVal5 = new ParameterDiscreteValue();
ParameterDiscreteValue discreteVal6 = new ParameterDiscreteValue();
ParameterDiscreteValue discreteVal7 = new ParameterDiscreteValue();
ParameterDiscreteValue discreteVal8 = new ParameterDiscreteValue();
ParameterRangeValue rangeVal = new ParameterRangeValue();
// 设置参数字段的名称,它必须
//和报表中的参数相符。
paramField1.ParameterFieldName = "year";
paramField2.ParameterFieldName = "month";
paramField3.ParameterFieldName = "day";
paramField4.ParameterFieldName = "Hour";
paramField5.ParameterFieldName = "Minute";
paramField6.ParameterFieldName = "sname";
paramField7.ParameterFieldName = "Amount";
paramField8.ParameterFieldName = "CNAmount";
// 设置第一个离散值并将其传递给该参数。
discreteVal1.Value = dt.Rows[0]["TIME"].ToString().Substring(0, 4);
discreteVal2.Value = dt.Rows[0]["TIME"].ToString().Substring(4, 2);
discreteVal3.Value = dt.Rows[0]["TIME"].ToString().Substring(6, 2);
discreteVal4.Value = dt.Rows[0]["TIME"].ToString().Substring(8, 2);
discreteVal5.Value = dt.Rows[0]["TIME"].ToString().Substring(10, 2);
discreteVal6.Value = dt.Rows[0]["name"].ToString();
string Amount = (Convert.ToDouble(dt.Rows[0]["PRC"].ToString()) * Convert.ToDouble(dt.Rows[0]["VOL"].ToString())).ToString();
decimal AmountN = 0;
discreteVal8.Value = PublicClass.ConvertToCn(Amount,out AmountN);
discreteVal7.Value = AmountN;
paramField1.CurrentValues.Add(discreteVal1);
paramField2.CurrentValues.Add(discreteVal2);
paramField3.CurrentValues.Add(discreteVal3);
paramField4.CurrentValues.Add(discreteVal4);
paramField5.CurrentValues.Add(discreteVal5);
paramField6.CurrentValues.Add(discreteVal6);
paramField7.CurrentValues.Add(discreteVal7);
paramField8.CurrentValues.Add(discreteVal8);
// 将该参数添加到参数字段集合。
paramFields.Add(paramField1);
paramFields.Add(paramField2);
paramFields.Add(paramField3);
paramFields.Add(paramField4);
paramFields.Add(paramField5);
paramFields.Add(paramField6);
paramFields.Add(paramField7);
paramFields.Add(paramField8);
// 将参数字段集合放入查看器控件。
CrystalReportViewer1.ReportSource = customerReport;
this.CrystalReportViewer1.ParameterFieldInfo = paramFields;

//设置成 139.7 x 215.9 毫米的纸张,也就是“半刀纸”,约 A4 的一半。
customerReport.PrintOptions.PaperSize = CrystalDecisions.Shared.PaperSize.PaperStatement;
//设置报表方向为横向,注意系统没有真正的“半刀纸”,只有把上述纸张横着看才符合要求。打印时不需要设置为横打
customerReport.PrintOptions.PaperOrientation = CrystalDecisions.Shared.PaperOrientation.Landscape;

CrystalReportViewer1.HasCrystalLogo = false;
CrystalReportViewer1.HasToggleGroupTreeButton = false;
CrystalReportViewer1.HasViewList = false;
try
{
//CrystalReportViewer1.PrintMode = CrystalDecisions.Web.PrintMode.ActiveX;
System.Drawing.Printing.PrintDocument printDocument = new System.Drawing.Printing.PrintDocument();
customerReport.PrintOptions.PrinterName = printDocument.PrinterSettings.PrinterName;
customerReport.PrintToPrinter(1, true, 0, 0);
}
catch
{
}

上一个:c# 用linq方法怎么通过datagridview修改数据?
下一个:javascript的语法和C#怎么那么像呢?

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,