我在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
不管怎么说,如果您有更多问题,我可以继续这几天。要做到这一点,有很多事情要做。