2010年7月20日 星期二

Crystal Report 子報表

參考  http://community.csdn.net/Expert/topic/3463/3463941.xml?temp=.3613092

問題已解決,我把原作者用C#寫的代碼改成了vb.net,與大家共分享

在page_load中加載如下代碼,一切正常


Dim prtTran As New DataAccess.Transport


'設置主表
Dim dsTran As DataSet = prtTran.TraGetByTraPKID(curr_TraPKID)
Dim prtTranTable As DataTable = dsTran.Tables(0)
'dsTran.Tables.Remove(prtTranTable)
Dim rptTran As New crTran
rptTran.SetDataSource(prtTranTable)

'設置子表()
Dim dsTranDetail As DataSet = prtTran.TrdGetByTraPKID(curr_TraPKID)
Dim prtTranDetailTable As DataTable = dsTranDetail.Tables(0)
'dsTranDetail.Tables.Remove(prtTranDetailTable)

Dim rptTranDetail As New crTranDetail
rptTranDetail.SetDataSource(prtTranDetailTable)


'設置登錄信息

Dim crSections As CrystalDecisions.CrystalReports.Engine.Sections
Dim crReportObjects As CrystalDecisions.CrystalReports.Engine.ReportObjects
Dim crSubreportObject As CrystalDecisions.CrystalReports.Engine.SubreportObject
Dim crDatabase As CrystalDecisions.CrystalReports.Engine.Database
Dim crTables As CrystalDecisions.CrystalReports.Engine.Tables
Dim crConnectioninfo As CrystalDecisions.Shared.ConnectionInfo = New CrystalDecisions.Shared.ConnectionInfo
Dim crSubreportDocument As ReportDocument

Dim tbl As CrystalDecisions.CrystalReports.Engine.Table
Dim logOnInfo As New TableLogOnInfo

'設置logOnInfo參數
For Each tbl In rptTran.Database.Tables

logOnInfo = tbl.LogOnInfo
logOnInfo.ConnectionInfo.ServerName = "localhost "
logOnInfo.ConnectionInfo.DatabaseName = "your db "
logOnInfo.ConnectionInfo.UserID = "sa "
logOnInfo.ConnectionInfo.Password = "yourpassword "
tbl.ApplyLogOnInfo(logOnInfo)
Next

crSections = rptTran.ReportDefinition.Sections
Dim crSection As Section
For Each crSection In crSections

crReportObjects = crSection.ReportObjects
Dim crReportObject As ReportObject
For Each crReportObject In crReportObjects

If (crReportObject.Kind = ReportObjectKind.SubreportObject) Then

crSubreportObject = CType(crReportObject, SubreportObject)
crSubreportDocument = crSubreportObject.OpenSubreport(crSubreportObject.SubreportName)
crDatabase = crSubreportDocument.Database
crTables = crDatabase.Tables

Dim crTable As CrystalDecisions.CrystalReports.Engine.Table
For Each crTable In crTables

crConnectioninfo.ServerName = "Localhost "
crConnectioninfo.UserID = "sa "
crConnectioninfo.Password = "your_password "
crConnectioninfo.DatabaseName = "your_db "
logOnInfo = crTable.LogOnInfo
logOnInfo.ConnectionInfo = crConnectioninfo
crTable.ApplyLogOnInfo(logOnInfo)
Next
End If
Next
Next


crvTransport.ReportSource = rptTran
crvTransport.Zoom(2)


prtTranDetailTable.DataSet.Dispose()
prtTranTable.DataSet.Dispose()

沒有留言:

張貼留言