我在 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 中
- 使用在 RDLC 文件中指定的相同名称将数据源添加到 ReportViewer 控件。如果名称不匹配,则会出现错误。
[代码从这里开始 - 代码块搞砸了,无法修复。]
Public Sub DisplayReport(ByVal ms As MemoryStream, ByVal ds As DataSet)
Dim RowCount As Integer = 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
无论如何,如果您还有更多问题,我可以继续讨论几天。要让它运行起来,还有很多工作要做。