将数据表传递给 ReportViewer [英] Pass DataTable to ReportViewer

查看:26
本文介绍了将数据表传递给 ReportViewer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数据表传递给我通过代码填充的报表查看器,有没有办法做到这一点?我试过了,但什么也没发生:

I am trying to pass a datatable to a reportviewer which I fill by code, is there a way to do that? I tried this but nothing happened:

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:GridView 与表格Tablefillbycode"配合良好

PS : The GridView works fine with the table "Tablefillbycode"

推荐答案

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

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

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

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

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

Put a report viewer on your Form2 and set its Dock property to Fill and set its Modifier property to Public.

Form1 中,我想你有一个 DataGridView1,你想在 Form_Load 中填充它,你将使用相同的您用于创建报告的查询.

In Form1 I suppose you have a DataGridView1 that you want to fill it in the Form_Load and you will use the same query that you used for creating report.

Form1 中,我想你有一个 Button1,当你点击 Button1<时你想显示 Form2/code> 并且您希望将 DataGridView1 的数据传递给它.

In Form1 I suppose you have a Button1 that you want to show Form2 when you click on Button1 and you want pass the data of DataGridView1 to it.

不要忘记在Form1

代码:

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

截图:

这篇关于将数据表传递给 ReportViewer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆