問題
自訂義Dll CrystalReport Parameter 繫結分享
解決方法
以下class是我用來開發WinForm時所自己寫的
使用方式如下:
//實體化 SilanoLibForWin.CrystalReportLib cr = new SilanoLibForWin.CrystalReportLib(); //rpOrderForm是CrystalReport //myFormDS是DataSet rpOrderForm.SetDataSource(myFormDS); //清除參數 cr.ClearParameter(); //傳入參數,注意,對應到報表的參數史以"參數的順序",名稱不重要 cr.SetReportParameter(mLoginUser); //傳入值 cr.SetReportParameter(""); //傳入要繫結的報表 cr.BindReport(rpOrderForm);程式碼如下:
using System; using System.Collections.Generic; using System.Text; using CrystalDecisions.Shared; using CrystalDecisions.CrystalReports.Engine; namespace SilanoLibForWin { /// <summary> /// 提供報表設定參數 /// </summary> public class CrystalReportLib { private List<string> mReportParamValue = new List<string>(); /// <summary> /// 步驟1: /// 清除以繫結參數 /// </summary> public void ClearParameter() { mReportParamValue.Clear(); } /// <summary> /// 步驟2: /// 設定報表參數 /// </summary> /// <param name="ParamValue"></param> public void SetReportParameter( string ParamValue) { mReportParamValue.Add(ParamValue); } /// <summary> /// 步驟3: /// 傳入報表以繫結參數 /// </summary> /// <param name="mReportFile">傳回已繫結後的報表</param> public void BindReport(ReportDocument mReportFile) { int cnt = mReportFile.DataDefinition.ParameterFields.Count; for (int i = 0; i < cnt; i++) { ParameterValues myvals = new ParameterValues(); ParameterDiscreteValue myDiscrete = new ParameterDiscreteValue(); switch (mReportFile.DataDefinition.ParameterFields[i].ParameterValueKind) { case CrystalDecisions.Shared.ParameterValueKind.DateTimeParameter: myDiscrete.Value = Convert.ToDateTime(mReportParamValue[i].ToString()); break; case CrystalDecisions.Shared.ParameterValueKind.BooleanParameter: myDiscrete.Value = bool.Parse(mReportParamValue[i].ToString()); break; case CrystalDecisions.Shared.ParameterValueKind.DateParameter: myDiscrete.Value = Convert.ToDateTime(mReportParamValue[i].ToString()); break; case CrystalDecisions.Shared.ParameterValueKind.NumberParameter: myDiscrete.Value = int.Parse(mReportParamValue[i].ToString()); break; case CrystalDecisions.Shared.ParameterValueKind.StringParameter: myDiscrete.Value = Convert.ToString(mReportParamValue[i].ToString()); break; } myvals.Add(myDiscrete); mReportFile.DataDefinition.ParameterFields[i].ApplyCurrentValues(myvals); } } } }
2 則留言:
Took me time to read the whole article, the article is great but the comments bring more brainstorm ideas, thanks.
- Johnson
Thanks for your response!
You inspire me too!!
-Silano
張貼留言