问题
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"
The ReportViewer
回答1:
Follow these steps to be able to pass data table to your report:
I suppose you created a
Report1.rdlc
in root of your projectTest
, so the name of its embedded resource would beTest.Report1.rdlc
. Also I suppose the name ofDataSet
in yourReport1
isDataSet1
.Put a report viewer on your
Form2
and set itsDock
property toFill
and set itsModifier
property toPublic
.In
Form1
I suppose you have aDataGridView1
that you want to fill it in theForm_Load
and you will use the same query that you used for creating report.In
Form1
I suppose you have aButton1
that you want to showForm2
when you click onButton1
and you want pass the data ofDataGridView1
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:
来源:https://stackoverflow.com/questions/34511856/pass-datatable-to-reportviewer