【问题标题】:VS 2013 ReportViewer only showing first rowVS 2013 ReportViewer 仅显示第一行
【发布时间】:2016-09-08 13:16:39
【问题描述】:

我正在尝试使用 ReportViewer 来运行包含从 datagridview 填充的数据集的报表。我可以填充数据集,但是当我运行报告时,它只显示第一行。我对此进行了无数小时的搜索,但似乎没有任何效果。

  • 我已确保 =First(...) 已从我的报告字段表达式中删除
  • 我认为问题与数据集的 XML 有关,但我不是很熟悉。
  • 这只是一个例子,一旦我弄清楚了,我就可以将我的知识转移到我的实际项目中。

这是我所拥有的:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    dgv.Columns.Add("FirstName", "First Name")
    dgv.Columns.Add("LastName", "Last Name")
    dgv.Rows.Add("John", "Smith")
    dgv.Rows.Add("Jane", "Doe")
    createData()

End Sub

Private Sub createData()

    Dim dt As New DataTable("namesTable")

    dt.Columns.Add("FirstName")
    dt.Columns.Add("LastName")

    For Each row As DataGridViewRow In dgv.Rows
        dt.Rows.Add(row.Cells(0).Value, row.Cells(1).Value)
        MsgBox(row.Cells(0).Value & " " & row.Cells(1).Value)
    Next
    MsgBox(dt.Rows.Count)

    DataSet1.Tables.Add(dt)

    MsgBox(DataSet1.GetXml)

    Dim DSReport As New ReportDataSource()
    DSReport.Name = "DataSet1"
    DSReport.Value = DataSet1.Tables("namesTable")

    ReportViewer.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local
    ReportViewer.LocalReport.ReportEmbeddedResource = "Report1.rdlc"
    ReportViewer.LocalReport.DataSources.Clear()
    ReportViewer.LocalReport.DataSources.Add(DSReport)
    ReportViewer.LocalReport.Refresh()
    ReportViewer.RefreshReport()

End Sub

当我添加到 dt 时,我的 MsgBox 消息正确地显示了我的两个名字。 MsgBox dt 计数为 2,因此添加了我的两个名称。

Dataset XML 显示了这一点,我认为这是问题所在,但我不知道如何解决:

DataSet1 XML

然后报告显示:

ReportViewer

感谢您的帮助,这让我发疯了。

谢谢!

【问题讨论】:

    标签: vb.net datagridview dataset reportviewer


    【解决方案1】:

    看起来您的数据源不正确,为什么不直接使用您刚刚创建的数据集。这段代码对我有用:

            ReportViewerHeader.LocalReport.DataSources.Clear()
            Dim rds As New Microsoft.Reporting.WebForms.ReportDataSource("namesTable", dt)
            ReportViewerHeader.LocalReport.ReportPath = "Report1.rdlc"
            ReportViewerHeader.LocalReport.DataSources.Add(rds)
            ReportViewerHeader.DataBind()
            ReportViewerHeader.LocalReport.Refresh()
    

    【讨论】:

    • 它必须首先根据我的理解进行转换。无论如何我都试过了,得到消息“'WindowsApplication1.DataSet1'类型的值不能转换为'Microsoft.Reporting.WinForms.ReportDataSource'
    • 我从一个工作程序中添加了一些代码,它应该能让你接近,但你可能需要调整它。
    • 谢谢,查克。 “DataBind”对我来说不存在,你为此导入了什么,如果有的话?如果我只是注释掉 DataBind,它会运行,但 ReportViewer 是完全空白的。如果我在代码末尾添加 RefreshReport() 它告诉我数据源实例尚未提供给数据源 DataSet1 。
    猜你喜欢
    • 1970-01-01
    • 2011-11-13
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 2013-10-16
    • 2011-09-14
    相关资源
    最近更新 更多