vb.net 将 DataTable 绑定到 RDLC 和 ReportViewer

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21680733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:25:01  来源:igfitidea点击:

Bind DataTable to RDLC and ReportViewer

vb.netdatatabledatasourcereportviewer

提问by Mark C.

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

我已经阅读了关于此的每一个 SO 问题和在线文章,我在几个不同的情况下感到困惑。

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

在我的项目中,我尝试Report2.rdlc手动创建报告 ( ) 并将不同的字段从DataSources报告中拖出,并将该报告用作ReportViewer. 这没有用。我需要使用DataTable我从 a 创建的,SqlDataAdapter因为它解密特定的行。我尝试创建 aDataSet并用 填充它DataTable,但是我也无法执行。

  1. I don't understand: if I have a ReportViewercontrol on a WinForm, what all I need in my code to bind a data source to it.
  2. Is the code evencreating a report and utilizing the report for the ReportViewer?
  1. 我不明白:如果我对 a 进行ReportViewer控制 WinForm,那么我的代码中需要什么才能将数据源绑定到它。
  2. 代码甚至创建报告并使用报告ReportViewer吗?

The DataTablename is dtand the ReportViewercontrol is rv1.

DataTable名称是dtReportViewer控制rv1

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

这是我一直在玩弄的代码,但是我不确定该放什么作为 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 ReportViewerjust shows up blank. There are no errors or any indicators as to what could be going wrong.

最糟糕的是,ReportViewer只显示空白。没有错误或任何指示可能会出错的地方。

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

中的信息DataTabledt全部正确(通过在 中查看来验证DGV)。我只是想在Report/ 中使用该数据ReportViewer

Does anyonehave 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.

有没有有什么建议吗?我似乎无法在这个问题上休息一下。注意:导出到 Excel 不是一种选择。有需要解密的加密值。报告需要可打印。

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

所以,这里我们有包含正确数据的 DataTable。然后我转到了项目名称,添加了一个新报告 (Report1.rdlc),从这里我不确定接下来的步骤。如果我动态创建一个数据集,我应该在这个窗口中看到它吗? 在此处输入图片说明



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)
    
  1. 右键单击您的项目 -> 添加新项目 -> 选择 Report.rdlc
  2. 左上角(数据源)-> 新建-> 数据集(选择数据库 [下一步 ->] 数据集 [下一步 ->] 您的数据库连接。
  3. “选择您的数据库对象”屏幕 -> 选择表
  4. “选择数据集”屏幕 -> 这将在运行时重置。请务必记住此数据集的名称,因为它将在代码中使用。
  5. 将 ReportViewer 控件(在 Reporting 下)添加到表单中。选择 reportviewer 控件并转到属性(VS 中的右下窗格)。选择 Local Report 的属性并将 ReportEmbeddedResource 设置为指向我们刚刚创建的报告路径。** 项目名称.报告名称.rdlc**
  6. 照常执行:

    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.

如果有什么我可以在这里发布以帮助其他人的,请告诉我。

采纳答案by peterG

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.

需要检查的一些事项:“测试报告”必须是报告设计器中使用的名称。它区分大小写。dt 也必须与报表设计器中使用的名称匹配“您的报表路径”的格式应为“namespace.reportname.rdlc”。它区分大小写。

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

编辑:我通常使用的方法以 BindingSource 的形式使用额外的层,如下所示:使用临时查询和 SqlDataAdapter 填充强类型数据集。将 Bindingsource 变暗并将其 DataSource 设置为数据集并将其 DataMember 设置为表名称。Dim 一个 ReportDataSource 并将其名称设置为报表设计器中使用的名称,并将其值设置为 BindingSource 例如

 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.

编辑 2:在屏幕截图中,下拉数据源并选择您想要的数据源。然后它会出现在“报表数据”窗口中,该窗口可能隐藏在“查看”菜单中(只有在您处理报表时才会出现)然后您可以从工具箱向报表添加一个表格,然后拖动字段从报表数据窗口到表格单元格。

回答by dovla091

something similar I have made for my "report loader", please check this link, it is written in C# but it will give you an idea how to do it.

我为我的“报告加载器”做了类似的事情,请检查这个链接,它是用 C# 编写的,但它会让你知道如何去做。

How to bind dynamically datasource to reportviewer on windows forms c#

如何在windows窗体c#上动态绑定数据源到reportviewer