vb.net Reportviewer 缺少数据源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14964401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reportviewer Missing Datasource
提问by Alex
I created a dataset which contains a table from my SQL Database named "BillHeaders"
我创建了一个数据集,其中包含我的 SQL 数据库中名为“BillHeaders”的表
Here's what my report viewer looks like, it will contain two reports. One that holds a job number and description, the other that holds a job number, description and contract number.

这是我的报告查看器的外观,它将包含两个报告。一个保存工作编号和描述,另一个保存工作编号、描述和合同编号。

Here are my buttons that will execute either report
这是我将执行任一报告的按钮


Here are my reports in my "Reports" folder. Both constructed and ready to go.

这是我的“报告”文件夹中的报告。两者都已建成并准备就绪。

Unfortuantely when I use this code (respectively for each button):
不幸的是,当我使用此代码时(分别针对每个按钮):
Private Sub btnJobNoDesc_Click(sender As System.Object, e As System.EventArgs) Handles btnJobNoDesc.Click
'Reset the viewer
frmReportViewer.ReportViewer1.Reset()
'Dim the required datasources. Need a seperate ReportDatasource for each table in the report
Dim ReportDataSource1 As Microsoft.Reporting.WinForms.ReportDataSource = New Microsoft.Reporting.WinForms.ReportDataSource
'Give datasource name and set the specific datatables
ReportDataSource1.Name = "dsBillHeaders_BillHeaders"
ReportDataSource1.Value = frmReportViewer.dsBillHeaders.BillHeaders
'Clear the datasources in the report and add the new ones
frmReportViewer.ReportViewer1.LocalReport.DataSources.Clear()
frmReportViewer.ReportViewer1.LocalReport.DataSources.Add(ReportDataSource1)
frmReportViewer.ReportViewer1.LocalReport.ReportEmbeddedResource = "ReportViewer_Tutorial.rptJobNoDesc.rdlc"
frmReportViewer.ReportViewer1.RefreshReport()
frmReportViewer.Show()
End Sub
I get this result:

我得到这个结果:

What am I doing wrong with my datasource?
我的数据源做错了什么?
回答by PatFromCanada
For one you are not setting the processing mode to local
对于一个,您没有将处理模式设置为本地
Here is code that works, when I create the report I make sure to have the datasource name in the report correspond to the table name of my dataset, not sure if that is required but it keeps things simpler.
这是有效的代码,当我创建报告时,我确保报告中的数据源名称与我的数据集的表名称相对应,不确定是否需要这样做,但它使事情变得更简单。
_reportViewerNewContracts.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local
_reportViewerNewContracts.LocalReport.ReportPath = "Reports\NewContractsReport.rdlc"
Dim reportDataSource1 As New Microsoft.Reporting.WinForms.ReportDataSource()
reportDataSource1.Name = _contractDataset.NewContracts.TableName
'Name of the report dataset in our .RDLC file
reportDataSource1.Value = _contractDataset.NewContracts
Me._reportViewerNewContracts.LocalReport.DataSources.Add(reportDataSource1)
'fill data
_reportViewerNewContracts.RefreshReport()
(I have seen lots of sites that recommend the datasetname underscore tablename but that never worked for me)
(我已经看到很多网站推荐使用 datasetname 下划线表名,但对我来说从来没有用过)
回答by Alex
Here's what I figured out:
这是我想出的:
Private Sub btnJobNoDesc_Click(sender As System.Object, e As System.EventArgs) Handles btnJobNoDesc.Click
'Reset the form
Dim rv As New frmReportViewer
'Reset the viewer
rv.ReportViewer1.Reset()
Dim ds As New dsBillHeaders
Dim ta As New dsBillHeadersTableAdapters.BillHeadersTableAdapter
ta.Fill(ds.BillHeaders)
rv.ReportViewer1.LocalReport.ReportEmbeddedResource = "ReportViewer_Tutorial.rptJobNoDesc.rdlc"
rv.ReportViewer1.LocalReport.DataSources.Clear()
Dim sReportDataSource As ReportDataSource
sReportDataSource = New ReportDataSource()
sReportDataSource.Name = "dsBillHeaders"
sReportDataSource.Value = ds.BillHeaders
rv.ReportViewer1.LocalReport.DataSources.Add(sReportDataSource)
rv.ReportViewer1.RefreshReport()
rv.Show()
End Sub
Using this code, I can generate multiple forms just by changing the name of the report on the ReportEmbeddedResource, sReportDataSource.Value and the dataset if necessary.
使用此代码,我可以通过更改 ReportEmbeddedResource、sReportDataSource.Value 和数据集(如有必要)上的报告名称来生成多个表单。

