Pass DataTable to ReportViewer
Asked Answered
C

1

3

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

Cierracig answered 29/12, 2015 at 13:13 Comment(7)
I think BindingSource is going to return a DataView object, not DataTable, which the Viewer needs (correct me if I'm wrong here). Try "Dim oDt as DataTable = DirectCast(bs.DataSource,DataView).ToTable()" And then "rs.Value = oDt" and let me know if it works.Seamark
What's the name of DataSet in your report?Belorussia
@Seamark thanks i will try it nowCierracig
@RezaAghaei DataSet1Cierracig
@Seamark : Unable to cast object of type 'System.Data.DataTable' to type 'System.Data.DataView'.Cierracig
My apologies. I was almost positive the bindingsource would return a view.Seamark
@Seamark Its fine thanks anwCierracig
B
3

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

Any idea for creating RDLC report at run-time from a DataGridView?

You might be interested to the following post which explains how you can dynamically convert any DataGridView to an RDLC report at run-time for printing purpose, etc. The post comes withe the full code in a GitHub repo and a download link:

Belorussia answered 29/12, 2015 at 13:55 Comment(16)
Probably you set the ReportPath in LocalReport property of ReportViewer1 in designer. Just drop a ReportViewer on Form2 and use it without any extra settings.Belorussia
You can set the report source also using the designer, but then the way you can pass the table to your report is somehow different. Let me know if you tried it using a 1ReportViewer1` without extra settings or if you have any setting on it.Belorussia
Test.Report1.rdls should be .rdlc its a silly fault .. anw now i try what u told me to do the ReportViewer is not like the picture in the question (Disabled ) .. but its an empty Reportviewer!Cierracig
Just an information i know its late the DataTablefillbycode is a data table witch i create it from a database in my sql management studio, so it dasnot make sense if you told the ReportDataSource to takes source from the DataSet1 because the DataTablefillbycode is created by code and its not showing in the DataSet1 DiagrameCierracig
As I told, I supposed it is an instance of a table (which is filled with data)Belorussia
oh yeah thanks ,but how i can fix the problem any idea?Cierracig
Let me know what's the problem now and also share the code of creating and filling datatable.Belorussia
Can i just do that ? DataSet1.Tables.Add(DataTablefillbycode ) And then use ur way?Cierracig
Is the table that you create report for, is a member of your DataSet1?Belorussia
No , is there way to add it to be a member ?Cierracig
You don't need to do that. Just fill a data table using the query that you created your report and then set the table as I did above.Belorussia
Look Do you have an email or something i try to find the answer on google but nothing is clear ! I Try what u tell me to do but the reportviewer still blank, my datatable is not in dataset because its formed on vb and he takes multiples informations form differents tables in the datasetCierracig
I Read it and i do the same as u asking me to do the difference between your code and mine : 1- You fill exactly your table from sql to vb, mine is mixed i make a query in tableadabptar and it join more than table 2- the columns names are not the same in sql and in the vb datatable because i change it manualy and i put a data in the rows from the mixed table that i make it from the queryCierracig
1- Having a complex mixed query is not a problem. 2- you should use the same column names in your query result that you used in your report. In fact the schema of the query that you used for report and the data table you want to pass to the report should be same. 3- You can simply use the same query that you create in your VB application for your report.Belorussia
Thanks a lot , I will try to add the datatable witch i create to database then fill it againCierracig
You can also set the report source using designer. You may find this answer helpful.Belorussia

© 2022 - 2024 — McMap. All rights reserved.