此示例将包含三个 DataSets
. 由于我们有三个数据集,我将使用三合一 DataSources
Report.rdlc 。该场景将是一条简单的生产线。我们将拥有启动机器、正在组装的机器以及即将发货的机器。
我假设您知道如何创建 DataSets (.XSD) 文件。我会将代码分成几部分,并在这篇文章中向您展示最终结果。
对您的问题重要的是我如何将三个数据源分配给一份报告。
声明
我有三个自定义类 , clsAssemblyState
,clsExpeditionState
它们clsStartState
保存从数据库收集的信息。这些只是我用作对象的自定义类。您将数据与 XML 绑定,我以编程方式分配它,您将在下面的“以编程方式填充数据表”部分中看到。
Dim ds1 As New dsAssemblies 'Link to my DataSet called dsAssemblies'
Dim ds2 As New dsExpeditions 'Link to my DataSet called dsExpeditions'
Dim ds3 As New dsStarts 'Link to my DataSet called dsStarts'
Dim sReportDataSource1 As ReportDataSource 'First datasource'
Dim sReportDataSource2 As ReportDataSource 'Second datasource'
Dim sReportDataSource3 As ReportDataSource 'Third datasource'
Dim AssemblyStates As List(Of clsAssemblyState) = clsAssemblyState.GetAll() 'List that contains all my machines being assembled'
Dim ExpeditionStates As List(Of clsExpeditionState) = clsExpeditionState.GetAll() 'List that contains all my machines in shipping'
Dim StartStates As List(Of clsStartState) = clsStartState.GetAll() 'List of all my machines being started (paper work)'
重置报告
在分配新数据源之前清除报表数据源是一种很好的做法。
'Reset the viewer'
rv.Reset()
rv.LocalReport.ReportEmbeddedResource = "YourProjectName.YourReportName.rdlc"
rv.LocalReport.DataSources.Clear()
sReportDataSource1 = New ReportDataSource()
sReportDataSource2 = New ReportDataSource()
sReportDataSource3 = New ReportDataSource()
以编程方式填充数据表
这将填充我的 dsAssemblies 的 DataTable,其他两个 DataTable 也是如此,但我在这里写出来是多余的。
'Fill datatables'
If AssemblyStates.Count > 0 Then
For Each asmState As clsAssemblyState In AssemblyStates
Dim asm As New clsAssembly(asmState.FK_Assembly)
Dim Machine As New clsMachine(asm.FK_Machine)
Dim Client As New clsClient(Machine.FK_Client)
Dim State As New clsState(asmState.FK_State)
ds1.dtAssembly.Rows.Add(Machine.MachineNo, Machine.Description, Client.Nom, State.State, asm.DateTransfer.ToString("yyyy-MM-dd"))
Next
Else
'No information was retrieved from my GetAll(), therefor no rows ... I add a row with values of "N/A" notifying the user that there is nothing in that particular DataTable'
ds1.dtAssembly.Rows.Add("N/A", "N/A", "N/A", "N/A", "N/A")
End If
将数据表分配给数据源
这sReportDataSource[i].Name
是我分配给其各自数据集的每个数据表的名称。我将在代码下方显示数据集的图片。
sReportDataSource1.Name = "Assembly_DataSet"
sReportDataSource2.Name = "Expedition_DataSet"
sReportDataSource3.Name = "Start_DataSet"
sReportDataSource1.Value = ds1.dtAssembly
sReportDataSource2.Value = ds2.dtExpedition
sReportDataSource3.Value = ds3.dtStart
rv.LocalReport.DataSources.Add(sReportDataSource1)
rv.LocalReport.DataSources.Add(sReportDataSource2)
rv.LocalReport.DataSources.Add(sReportDataSource3)
rv.RefreshReport()
Me.Show()
在这里您可以看到三个 DataSet 及其 DataTables
完整报告代码
此方法将使用三个数据源生成报告。当然,您必须将这些数据集分配给您的某些东西,.rdlc
例如三个不同的 tablix。
Private Sub GenerateStatusProduction()
Dim ds1 As New dsAssemblies
Dim ds2 As New dsExpeditions
Dim ds3 As New dsStarts
Dim sReportDataSource1 As ReportDataSource
Dim sReportDataSource2 As ReportDataSource
Dim sReportDataSource3 As ReportDataSource
Dim AssemblyStates As List(Of clsAssemblyState) = clsAssemblyState.GetAll()
Dim ExpeditionStates As List(Of clsExpeditionState) = clsExpeditionState.GetAll()
Dim StartStates As List(Of clsStartState) = clsStartState.GetAll()
'Reset the viewer'
rv.Reset()
rv.LocalReport.ReportEmbeddedResource = "YourProjectName.YourReportName.rdlc"
rv.LocalReport.DataSources.Clear()
sReportDataSource1 = New ReportDataSource()
sReportDataSource2 = New ReportDataSource()
sReportDataSource3 = New ReportDataSource()
'Fill datatables'
If AssemblyStates.Count > 0 Then
For Each asmState As clsAssemblyState In AssemblyStates
Dim asm As New clsAssembly(asmState.FK_Assembly)
Dim Machine As New clsMachine(asm.FK_Machine)
Dim Client As New clsClient(Machine.FK_Client)
Dim State As New clsState(asmState.FK_State)
ds1.dtAssembly.Rows.Add(Machine.MachineNo, Machine.Description, Client.Nom, State.State, asm.DateTransfer.ToString("yyyy-MM-dd"))
Next
Else
ds1.dtAssembly.Rows.Add("N/A", "N/A", "N/A", "N/A", "N/A")
End If
If ExpeditionStates.Count > 0 Then
For Each expdState As clsExpeditionState In ExpeditionStates
Dim Expd As New clsExpedition(expdState.FK_Expedition)
Dim Machine As New clsMachine(Expd.FK_Machine)
Dim Client As New clsClient(Machine.FK_Client)
Dim State As New clsState(expdState.FK_State)
ds2.dtExpedition.Rows.Add(Machine.MachineNo, Machine.Description, Client.Nom, State.State, Expd.DateTransfer.ToString("yyyy-MM-dd"))
Next
Else
ds2.dtExpedition.Rows.Add("N/A", "N/A", "N/A", "N/A", "N/A")
End If
If StartStates.Count > 0 Then
For Each strtState As clsStartState In StartStates
Dim Strt As New clsStart(strtState.FK_Start)
Dim Machine As New clsMachine(Strt.FK_Machine)
Dim Client As New clsClient(Machine.FK_Client)
Dim State As New clsState(strtState.FK_State)
ds3.dtStart.Rows.Add(Machine.MachineNo, Machine.Description, Client.Nom, State.State, Strt.DateTransfer.ToString("yyyy-MM-dd"))
Next
Else
ds3.dtStart.Rows.Add("N/A", "N/A", "N/A", "N/A", "N/A")
End If
sReportDataSource1.Name = "Assembly_DataSet"
sReportDataSource2.Name = "Expedition_DataSet"
sReportDataSource3.Name = "Start_DataSet"
sReportDataSource1.Value = ds1.dtAssembly
sReportDataSource2.Value = ds2.dtExpedition
sReportDataSource3.Value = ds3.dtStart
rv.LocalReport.DataSources.Add(sReportDataSource1)
rv.LocalReport.DataSources.Add(sReportDataSource2)
rv.LocalReport.DataSources.Add(sReportDataSource3)
rv.RefreshReport()
Me.Show()
End Sub
注意:rv
是我的报表查看器控件名称而不是ReportViewer1