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

查看:34
本文介绍了将 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 填充它,但是我也无法执行.

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. 我不明白:如果我有一个 ReportViewer 控件WinForm,我需要在代码中将数据源绑定到它.
  2. 代码是否甚至创建报告并将报告用于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 名称为 dtReportViewer 控件为 rv1.

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)`.

最糟糕的是,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 中查看来验证).我只是想在 Report/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.

编辑:这是我填充数据表的方式:

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)

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

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

  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 的形式如下:使用临时查询和 SqlDataAdapter 填充强类型数据集.将 Bindingsource 变暗并将其 DataSource 设置为数据集并将其 DataMember 设置为表名称.将 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天全站免登陆