VS.Net中向报表模板中传递参数与公式

本文接:《直连数据库拉模式的水晶报表编程实现之极简教程

编辑上文之模板,增加一个参数myid,两个公式。公式xx1中是对参数myid进行操作。公式xx2内容为空,在程序中编程控制此公式。

 

全部文件代码段如下:

 

Imports CrystalDecisions.Shared
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Web

Partial Class _Default
    
Inherits System.Web.UI.Page
    
Private myReport As ReportDocument

    
Private Sub ConfigureCrystalReports()
        
Dim myConnectionInfo As ConnectionInfo = New ConnectionInfo()
        myConnectionInfo.DatabaseName 
= "xxx"
        myConnectionInfo.UserID 
= "xxx"
        myConnectionInfo.Password 
= "xxx"
        myConnectionInfo.ServerName 
= "xxx"

        myReport 
= New ReportDocument()
        
Dim reportPath As String = Server.MapPath("crystalreport1.rpt")


        
'加载报表文件
        myReport.Load(reportPath)

        
'-----------------------------------------------
        '传递公式字段
        '注意:公式字段在将rpt传递给crviewr控件前操作!
        '需要特别注意的是:如果你的公式返回的是一个文本,必须用单引号刮起来
        myReport.DataDefinition.FormulaFields("xx2").Text = "'这是一个公式'"
        '传递参数
        myReport.ParameterFields("myId").CurrentValues.AddValue(1001)
        
'-----------------------------------------------

        CrystalReportViewer1.ReportSource 
= myReport

        
''-----------------------------------------------
        ''传递报表参数的另一种写法,将参数传递给viewer
        'Dim paramFields = New ParameterFields()
        'Dim paramField = New ParameterField()
        'Dim discreteVal = New ParameterDiscreteValue()

        
''定位参数
        'paramField.ParameterFieldName = "myId"
        ''定义参数值
        'discreteVal.Value = 101
        ''赋值
        'paramField.CurrentValues.Add(discreteVal)
        'paramFields.Add(paramField)
        'CrystalReportViewer1.ParameterFieldInfo = paramFields
        ''-----------------------------------------------

        SetDBLogonForReport(myConnectionInfo, myReport)

    
End Sub

    
Private Sub SetDBLogonForReport(ByVal myConnectionInfo As ConnectionInfo, ByVal myReportDocument As ReportDocument)
        
Dim myTables As Tables = myReportDocument.Database.Tables
        
For Each myTable As CrystalDecisions.CrystalReports.Engine.Table In myTables
            
Dim myTableLogonInfo As TableLogOnInfo = myTable.LogOnInfo
            myTableLogonInfo.ConnectionInfo 
= myConnectionInfo
            myTable.ApplyLogOnInfo(myTableLogonInfo)
        
Next
    
End Sub

    
Protected Sub Page_Init(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Init
        ConfigureCrystalReports()
    
End Sub
End Class

 

 效果如下:
 

posted @ 2008-10-10 11:34  阿泰  阅读(2304)  评论(18编辑  收藏  举报