我在4個月的時間內完成了這項工作。我的代碼在VB.NET中,並且很長。我從GotReportViewer中列出的代碼開始,並在其上構建。概括地說,這是你需要做什麼:
- 渲染和內存RDLC文件 - 使用一個DataTable (或數據集,爲 多表報表)作爲輸入
對於本,我創建了一個名爲ReportEngine的類。它基本上只是一些創建RDLC文件的函數。這是整個操作的膽量,代碼很長。以下是我正在使用的一些主要功能。這將是最好只給你發郵件我的類 - 因爲他們是很長:
'Data Building variables
Private _reportDataset As DataSet 'Data displayed in report
Private _AllFields As List(Of String) 'All column field names
Private _AllCaptions As List(Of String) 'All column names to display in report (needed for french translation)
Private _reportRDL As MemoryStream 'Report definition file
Private _reportControl As ReportControl 'Control that displays the report
Public Sub LoadReport(ByVal reportDataset As DataSet)
Try
_reportDataset = reportDataset
'check if the datatable contains data
_hasNoData = False
If _reportDataset.Tables(0).Rows.Count = 0 Then
_hasNoData = True
End If
'Get table column fieldnames, captions and widths
_AllFields = GetTableFields(0)
_AllCaptions = GetTableCaptions(0)
'reset RDL file if already existing
If Not (_reportRDL Is Nothing) Then
_reportRDL.Dispose()
End If
GenerateRdl() 'Create the RDLC file
ShowReport() 'Load it into the ReportViewer Control
RaiseEvent ReportLoaded(Me) 'Indicate via event that report is loaded and ready to be displayed
Catch ex As Exception
'Handle error
End Try
End Sub
'returns a list of fields from a datatable used for the report
Public Function GetTableFields(ByVal tableIndex As Integer) As List(Of String)
Dim dataTable As DataTable = _reportDataset.Tables(tableIndex)
Dim availableFields As New List(Of String)
Dim i As Integer
For i = 0 To dataTable.Columns.Count - 1
availableFields.Add(dataTable.Columns(i).ColumnName)
Next i
Return availableFields
End Function
'returns a list of captions from a datatable
Public Function GetTableCaptions(ByVal tableIndex As Integer) As List(Of String)
Dim dataTable As DataTable = _reportDataset.Tables(tableIndex)
Dim captions As New List(Of String)
Dim i As Integer
For i = 0 To dataTable.Columns.Count - 1
captions.Add(dataTable.Columns(i).Caption)
Next i
Return captions
End Function
- 裝入RDLC文件到的ReportViewer從內存
- 數據源添加到ReportViewer控件,使用相同的名稱在RDLC文件中指定。如果名稱不匹配,則會出錯。
[代碼這裏開始 - 代碼塊搞砸了,不能修復]
昏暗的行數爲整數= 0
ReportViewer1.Reset()
ReportViewer1.LocalReport.DataSources.Clear()
ReportViewer1.LocalReport.LoadReportDefinition(ms)
For I As Integer = 0 To Me.ReportEngine.ReportDataSet.Tables.Count - 1
'Bind dataTables to the report viewer control - matches the datasources contained in the RDLC files
ReportViewer1.LocalReport.DataSources.Add(New ReportDataSource("MyData" + I.ToString, ds.Tables(I)))
'Calc total rows returned
RowCount += ds.Tables(I).Rows.Count
Next
SetupReport()
ReportViewer1.RefreshReport()
End Sub
不管怎麼說,如果您有更多問題,我可以繼續這幾天。要做到這一點,有很多事情要做。