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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:39:26  来源:igfitidea点击:

Pass DataTable to ReportViewer

vb.netwinformsdatatablereportviewer

提问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”配合良好

The ReportViewer

报表查看器

采纳答案by Reza Aghaei

Follow these steps to be able to pass data table to your report:

请按照以下步骤将数据表传递给您的报告:

  1. I suppose you created a Report1.rdlcin root of your project Test, so the name of its embedded resource would be Test.Report1.rdlc. Also I suppose the name of DataSetin your Report1is DataSet1.

  2. Put a report viewer on your Form2and set its Dockproperty to Filland set its Modifierproperty to Public.

  3. In Form1I suppose you have a DataGridView1that you want to fill it in the Form_Loadand you will use the same query that you used for creating report.

  4. In Form1I suppose you have a Button1that you want to show Form2when you click on Button1and you want pass the data of DataGridView1to it.

  1. 我想您在Report1.rdlc项目的根目录中创建了一个Test,因此其嵌入资源的名称将是Test.Report1.rdlc. 另外我想DataSet你的名字Report1DataSet1.

  2. 在您的上放置一个报告查看器Form2并将其Dock属性Fill设置为并将其Modifier属性设置为Public

  3. Form1我想你有一个DataGridView1你想填写它Form_Load,你将使用与创建报告相同的查询。

  4. 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:

截屏:

enter image description here

在此处输入图片说明