Crystal 报告仅显示最后一列

Crystal Reports Show Only The last Column

我想在 crystal 报告中显示 Table 但它只显示最后一列,这是我的代码:

Try
        Cursor = Cursors.WaitCursor
        Dim id As Integer = 1
        Dim Report1 As New PrintStockReport
        Dim dt As DataTable = New DBConnect().selectdata( _
              "SELECT StockTable.StockRef, StockTable.StockCat FROM StockTable;")

        
        'Report1.SetDataSource(dt)
        For i As Integer = 0 To dt.Rows.Count - 1
            Report1.SetParameterValue("StockID", id)
            Report1.SetParameterValue("StockRef", dt.Rows(i)(0).ToString)
            Report1.SetParameterValue("StockCat", dt.Rows(i)(1).ToString)
            id += 1
        Next
        CrystalReportViewer1.ReportSource = Report1
        Cursor = Cursors.Default
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

但是,当我设置 Datasource 它显示 " the report has no tables" 时,谁能向我解释为什么我会遇到这个问题?谢谢

Report1.SetDataSource(dt)

首先让我们修复您的 VB 代码。完全删除 For/Next 循环,然后取消注释它上面的行。您的代码现在应该如下所示:

    Try
        Cursor = Cursors.WaitCursor
        Dim id As Integer = 1
        Dim Report1 As New PrintStockReport
        Dim dt As DataTable = New DBConnect().selectdata( _
              "SELECT StockTable.StockRef, StockTable.StockCat FROM StockTable;")

        
        Report1.SetDataSource(dt)
        
        CrystalReportViewer1.ReportSource = Report1
        Cursor = Cursors.Default
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

这将正确地将您的数据table绑定到作为数据源的 Report1 对象。现在您需要通过从报表中删除参数字段来修复设计器中的报表。仅当您需要用户在运行时将输入传递到报表时才使用参数字段。例如,如果我正在制作一个向用户显示单个财务周期数据的报告,并且我希望它足够灵活以处理任何周期,我会使用参数字段来要求用户提供周期作为输入报告执行时。如果您查看报表设计器中的字段资源管理器,您应该会看到“数据库字段”。展开后,这应该会向您显示定义为报告数据库的 table 或 table。展开 table 将向您显示报告可用的字段列表。将您希望在报告中显示的字段拖到它们应该出现的部分。

完成后,您的报告应该可以工作了。不过,这假设您已在报表设计器的数据库专家中定义了数据库。