Crystal报表通过分配给报表的特定记录显示表的所有记录

时间:2015-10-13 13:25:05

标签: vb.net crystal-reports crystal-reports-2008

我正在开发一个Windows应用程序。我正在使用Crystal Reports,它运行得很好。我的问题是,Crystal报表没有根据查询返回的记录显示记录。

我根据用户输入编写了获取记录的查询。但是当我将集合绑定到报表时,它会显示数据库中存在的所有记录,而不是我收集的记录。

还有一件事,它在添加参数字段之前完美运行。如果我使用下面的代码行:

cr.SetDataSource(oBatches)

它正常工作,显示收集中的唯一记录,但提示手动输入参数。以下是代码。

Private Sub btnLoadBatch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadBatch.Click
    Try
        Me.Cursor = Cursors.WaitCursor
        Dim cr As New crMonthwiseBatch
        Dim oBatches As New Batches
        Dim Month As Integer = dtFrom.Value.Date.Month
        Dim StartDateForBatch As Date = New DateTime(dtBatchStartFromMonth.Value.Year, dtBatchStartFromMonth.Value.Month, "1")
        Dim DaysinMonths As Integer = System.DateTime.DaysInMonth(dtBatchStartFromMonth.Value.Year, dtBatchStartFromMonth.Value.Month)
        Dim EndDateForBatch = StartDateForBatch.AddDays(DaysinMonths)

        ----->      oBatches.LoadByQuery("CreatedDate >= #" + StartDateForBatch + "# and CreatedDate <= #" + EndDateForBatch + "#")

        Dim crParameterFieldDefinitions As ParameterFieldDefinitions
        Dim crParameterFieldDefinition As ParameterFieldDefinition
        Dim crParameterValues As New ParameterValues
        Dim crParameterDiscreteValue As New ParameterDiscreteValue

        crParameterDiscreteValue.Value = "Batch List of Month " + MonthName(dtBatchStartFromMonth.Value.Month) + " " + dtBatchStartFromMonth.Value.Year.ToString
        crParameterFieldDefinitions = cr.DataDefinition.ParameterFields
        crParameterFieldDefinition = crParameterFieldDefinitions.Item("MonthName")
        crParameterValues = crParameterFieldDefinition.CurrentValues
        crParameterValues.Clear()
        crParameterValues.Add(crParameterDiscreteValue)
        crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
        CrystalReportViewerMonthwiseBatch.ReportSource = cr
        CrystalReportViewerMonthwiseBatch.Refresh()
        Me.Cursor = Cursors.Arrow
    Catch ex As Exception
        Me.Cursor = Cursors.Arrow
        MessageBox.Show("Error :- " + ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub

1 个答案:

答案 0 :(得分:0)

在分配数据对象(如下面的代码中标记)后,我没有设置报告的DataSource。

Private Sub btnShowCustomer_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowCustomer.Click
    Try
        Me.Cursor = Cursors.WaitCursor
        Dim cr As New rptDatewiseCustomerReport
        Dim oCustomers As New Customers
        Dim StartDate As DateTime
        Dim EndDate As DateTime
        StartDate = dtFrom.Value.Date.AddDays(-1)
        EndDate = dtTO.Value.Date.AddDays(1)
        oCustomers.LoadByQuery("CreatedDate >= #" + StartDate.Date + "# and CreatedDate <= #" + EndDate.Date + "#")

        'Added Line below---->        
        cr.SetDataSource(oCustomers)

        Dim crParameterFieldDefinitions As ParameterFieldDefinitions
        Dim crParameterFieldDefinition As ParameterFieldDefinition
        Dim crParameterValues As New ParameterValues
        Dim crParameterDiscreteValue As New ParameterDiscreteValue
        Dim DatetimeTest As String = dtFrom.Value.Date.ToString("dd-MMM-yyyy").ToString + " To " + dtTO.Value.Date.ToString("dd-MMM-yyyy").ToString()

        crParameterDiscreteValue.Value = DatetimeTest
        crParameterFieldDefinitions = cr.DataDefinition.ParameterFields
        crParameterFieldDefinition = crParameterFieldDefinitions.Item("paraFromDate")
        crParameterValues = crParameterFieldDefinition.CurrentValues
        crParameterValues.Clear()
        crParameterValues.Add(crParameterDiscreteValue)
        crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
        CrystalReportViewer2.ReportSource = cr
        CrystalReportViewer2.Refresh()
        Me.Cursor = Cursors.Arrow
    Catch ex As Exception
        Me.Cursor = Cursors.Arrow
        MessageBox.Show("Error :- " + ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub