Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
874 views
in Technique[技术] by (71.8m points)

vb.net - Pass DataTable to 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 : The GridView works fine with the table "Tablefillbycode"

The ReportViewer

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

  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.

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

  3. 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.

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

Don't forget to Imports Microsoft.Reporting.WinForms in 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...