将DataTable传递给ReportViewer [英] Pass DataTable to ReportViewer
问题描述
我正在尝试将datatable传递给一个报表查看器,我用代码填写它,有没有办法呢?我尝试这个但没有发生
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中正常使用
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"
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:
这篇关于将DataTable传递给ReportViewer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!