Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

vb.net - Bind DataTable to RDLC and ReportViewer

I have read every single SO question and online article regarding this, and I get confused in a couple different instances.

As my project sits, I have tried to create a Report (Report2.rdlc) manually and drag different fields from DataSources onto the report, and use that report as the data source for the ReportViewer. This did not work. I need to use the DataTable I created from a SqlDataAdapterbecause it decrypts specific rows. I tried creating a DataSet and filling it with the DataTable, however I was unable to perform that, also.

  1. I don't understand: if I have a ReportViewer control on a WinForm, what all I need in my code to bind a data source to it.
  2. Is the code even creating a report and utilizing the report for the ReportViewer?

The DataTable name is dt and the ReportViewer control is rv1.

Here's the code I have been toying around with, however I am not sure what to put as the Report Path

    Dim RDS1 As ReportDataSource = New ReportDataSource("Test Report", dt)

    rv1.LocalReport.DataSources.Clear()
    rv1.ProcessingMode = ProcessingMode.Local
    rv1.LocalReport.EnableExternalImages = True
    rv1.LocalReport.ReportEmbeddedResource = "Your Report Path"
    rv1.LocalReport.DataSources.Add(RDS1)`.

The worst part is, the ReportViewer just shows up blank. There are no errors or any indicators as to what could be going wrong.

The information within the DataTable dt is all correct (verified by viewing it in a DGV). I am just trying to use that data in a Report / ReportViewer.

Does anyone have any advice? I cannot seem to catch a break on this issue. Note: Exporting to Excel is not an option. There are encrypted values that require decryption. The report needs to be printable.

Edit: Here is how I populate the DataTable:

        Dim cmd As New SqlCommand
        cmd.CommandText = "Select * FROM Participant " & _
                          "WHERE FIRST_NM_TXT = @searchFirst " & _
                          "OR LAST_NM_TXT = @searchLast"
        cmd.Parameters.AddWithValue("@searchFirst", SearchFirstTxt.Text)
        cmd.Parameters.AddWithValue("@searchLast", SearchLastTxt.Text)

    Dim adapter As New SqlDataAdapter(cmd)
    adapter.Fill(dt)

So, here we have the DataTable with the correct data in it. I have then gone to the Project Name, Added a new Report (Report1.rdlc), and from here I am unsure of the next steps. If I create a DataSet dynamically, should I see it in this window? enter image description here


Since this seems to be a popular thread, I'll explain how I was able to do this in quick/easy steps.

  1. Right click on your porject -> Add New Item -> Selection Report.rdlc
  2. Top left (Data Source) -> New -> Dataset (select Database [Next ->] Dataset [Next ->] Your DB connection.
  3. "Choose your database objects" screen -> Select Tables
  4. "Choose the dataset" screen -> This will be reset at run time. Make sure you remember the name of this Dataset, because it will be used in the code.
  5. Add ReportViewer control (under Reporting) to the form. Select reportviewer control and go to properties (bottom right pane in VS). Select Local Report's property and set ReportEmbeddedResource to point to the report path we just created. ** ProjectName.ReportName.rdlc**
  6. Peform the usual:

    Public DataSet FillDS()
    //Try Catch block & other code omitted
    Dim cmd As New SqlCommand
    cmd.CommandText = "Select * FROM Participant " & _
                      "WHERE FIRST_NM_TXT = @searchFirst " & _
                      "OR LAST_NM_TXT = @searchLast"
    cmd.Parameters.AddWithValue("@searchFirst", SearchFirstTxt.Text)
    cmd.Parameters.AddWithValue("@searchLast", SearchLastTxt.Text)
    
    Dim adapter As New SqlDataAdapter(cmd)
    adapter.Fill(dt)
    

Then, we bind the datasource at run time.

DataSet yourDS = FillDS();
ReportDataSource rds = New ReportDataSource("YourDataSetNameFromStep4", yourDS.Tables[0]);
yourReportViewerName.localreport.datasources.clear();
yourReportViewerName.localreport.datasources.add(rds);
yourReportViewerName.refreshreport();

If there is something I could post in here to help anyone else, let me know.

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

Some things to check: "Test Report" must be the name used in the report designer. It's case sensitive. The dt must match the name used in the report designer too "Your report Path" should be of the form "namespace.reportname.rdlc" . It's case sensitive.

EDIT: The approach I normally use utilises an extra layer, in the form of a BindingSource as follows: Fill a strongly-typed dataset using the ad-hoc query and a SqlDataAdapter. Dim a Bindingsource and set its DataSource to the dataset and its DataMember to the table name. Dim a ReportDataSource and set its name to the name used in the report designer, and its value to the BindingSource eg

 Using cn As New SqlConnection(gcs)
        cn.Open()
        Dim sa As New SqlDataAdapter(sql, cn)
        sa.SelectCommand.CommandTimeout = cGlobals.ReportTimeout
        sa.Fill(ds, "Foos")
    End Using
    bs.DataSource = ds
    bs.DataMember = "Foos"
    Dim rds As New ReportDataSource
    rds.Name = "dsFooList_Foos"
    rds.Value = bs
    rv1.LocalReport.DataSources.Add(rds)

    Me.Show()
    rv1.RefreshReport()

Edit 2: At the point in your screenshot, drop down the datasources and pick the one you want. It will then appear in the Report Data window, which may be hiding in the 'View' menu (it's only there when you're working with a report) Then you can add a Table to the report from the toolbox, and then drag the fields from the Report Data window to the table cells.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...