vb.net 将数据表传递给 ReportViewer
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34511856/
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
Pass DataTable to ReportViewer
提问by hakoum
I am trying to pass a datatable to a reportviewer wich I fill it by code , is there a way to do that ? i try this but nothing happen
我正在尝试将数据表传递给我用代码填充的报表查看器,有没有办法做到这一点?我试试这个,但什么也没发生
Dim bs As BindingSource
bs = New BindingSource()
bs.DataSource = DataTablefillbycode
Dim rs As ReportDataSource
rs = New ReportDataSource()
rs.Name = "Tabletest"
rs.Value = bs
form2.ReportViewer1.RefreshReport()
form2.ReportViewer1.Reset()
form2.ReportViewer1.LocalReport.ReportEmbeddedResource = "Test.Report1.rdlc"
form2.ReportViewer1.LocalReport.DataSources.Clear()
form2.ReportViewer1.LocalReport.DataSources.Add(rs)
form2.ReportViewer1.RefreshReport()
form2.ShowDialog()
PS : The GridView works fine with the table "Tablefillbycode"
PS:GridView 与表“Tablefillbycode”配合良好
采纳答案by Reza Aghaei
Follow these steps to be able to pass data table to your report:
请按照以下步骤将数据表传递给您的报告:
I suppose you created a
Report1.rdlcin root of your projectTest, so the name of its embedded resource would beTest.Report1.rdlc. Also I suppose the name ofDataSetin yourReport1isDataSet1.Put a report viewer on your
Form2and set itsDockproperty toFilland set itsModifierproperty toPublic.In
Form1I suppose you have aDataGridView1that you want to fill it in theForm_Loadand you will use the same query that you used for creating report.In
Form1I suppose you have aButton1that you want to showForm2when you click onButton1and you want pass the data ofDataGridView1to it.
我想您在
Report1.rdlc项目的根目录中创建了一个Test,因此其嵌入资源的名称将是Test.Report1.rdlc. 另外我想DataSet你的名字Report1是DataSet1.在您的上放置一个报告查看器
Form2并将其Dock属性Fill设置为并将其Modifier属性设置为Public。在
Form1我想你有一个DataGridView1你想填写它Form_Load,你将使用与创建报告相同的查询。在
Form1我想你有一个Button1你想Form2在你点击时显示的Button1,你想将数据传递DataGridView1给它。
Don't forget to Imports Microsoft.Reporting.WinFormsin Form1
别忘了Imports Microsoft.Reporting.WinForms进去Form1
Code:
代码:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim cn = "data source=(localdb)\v11.0;initial catalog=TestDB;integrated security=True;"
Dim cmd = "SELECT Id,Name FROM Category"
Dim adapter = New SqlDataAdapter(cmd, cn)
Dim table = New DataTable()
adapter.Fill(table)
Me.DataGridView1.DataSource = table
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim form2 = New Form2()
Dim rds= New ReportDataSource("DataSet1", Me.DataGridView1.DataSource)
form2.ReportViewer1.LocalReport.DataSources.Clear()
form2.ReportViewer1.LocalReport.DataSources.Add(rds)
form2.ReportViewer1.LocalReport.ReportEmbeddedResource = "Test.Report1.rdlc"
form2.ShowDialog()
End Sub
Screenshot:
截屏:


![vb.net 中 float[] 的等效数据类型是什么?](/res/img/loading.gif)