将DataTable绑定到RDLC和ReportViewer [英] Bind DataTable to RDLC and ReportViewer

查看:217
本文介绍了将DataTable绑定到RDLC和ReportViewer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了关于这一点的每一个SO问题和在线文章,我在几个不同的实例中感到困惑。

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

正如我的项目所在,我试图手动创建一个报告( Report2.rdlc ),并将不同的字段从 DataSources 拖到报表上,并将该报告用作 ReportViewer 的数据源。这没有办法。我需要使用我从 SqlDataAdapter 创建的 DataTable ,因为它解密了特定的行。我尝试创建一个 DataSet 并填写 DataTable ,但是我也无法执行。 p>

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. 我不明白:如果我在$ b $上有一个 ReportViewer b WinForm ,我的代码中需要什么来绑定数据源。

  2. 代码 / em>创建一个报告,并使用报告
    $ ReportViewer

  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?

DataTable 名称是 dt ReportViewer 控制是 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)`.

最糟糕的部分是, ReportViewer 显示空白。没有任何错误或任何指标出现什么问题。

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

DataTable dt 中的信息都是正确的(通过在 DGV 中查看验证)。我只想在报告 / ReportViewer 中使用该数据。

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.

是否有任何人有任何建议?在这个问题上我似乎不能休息一下。
注意:导出到Excel不是一个选项。有需要解密的加密值。报告需要打印。

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.

修改:这是我如何填充DataTable:

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)

所以,这里我们有DataTable中包含正确的数据。然后我去了项目名称,添加了一个新的报告(Report1.rdlc),从这里我不确定下一步。如果我动态创建一个DataSet,我应该在这个窗口中看到它吗?

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?


  1. 右键单击您的项目 - >添加新项目 - >选择Report.rdlc

  2. 左上角(数据源) - >新建 - >数据集(选择数据库数据集[Next - >]您的数据库连接

  3. 选择数据库对象屏幕 - >选择表

  4. 选择数据集屏幕 - >这将在运行时重置。确保您记住此数据集的名称,因为它将在代码中使用。

  5. 将ReportViewer控件(在报表下)添加到窗体中。选择reportviewer控件并转到属性(VS中的右下窗格)。选择Local Report的属性并设置ReportEmbeddedResource以指向我们刚刚创建的报告路径** ProjectName.ReportName。 rdlc **

  6. P通常情况下:

  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.

推荐答案

有些事情要检查:
测试报告必须是报表设计器中使用的名称。这是区分大小写的。
dt必须与报表设计器中使用的名称匹配
您的报告路径应该是namespace.reportname.rdlc的形式。它是区分大小写的。

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.

编辑:
我通常使用的方法使用一个额外的图层,BindingSource的形式如下:
填写一个使用ad-hoc查询和SqlDataAdapter的强类型数据集。
定义一个Bindingsource并将其DataSource设置为数据集,并将其DataMember设置为表名。
Dim a ReportDataSource,并将其名称设置为报表设计器中使用的名称,其值为BindingSource
例如

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()

编辑2:在屏幕截图中,下拉数据源并选择一个你想要的然后,它将显示在报表数据窗口中,该窗口可能隐藏在查看菜单中(仅当您使用报表时才会显示)。然后可以从工具箱中将表添加到报表中,然后拖动报表数据窗口中的字段到表格单元格。

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.

这篇关于将DataTable绑定到RDLC和ReportViewer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆