【问题标题】:DevExpress Report must filter BindingSource with Parameters at the datasource levelDevExpress Report 必须在数据源级别过滤带有参数的 BindingSource
【发布时间】:2013-08-01 13:04:44
【问题描述】:

我正在尝试将XtraReport 绑定到BindingSource(而不是Dataset),并希望在数据源中的值使用报表参数到达报表之前对其进行过滤。

我已经在报表设计器中声明了参数和绑定源。所以我已经设置了字段和所有内容。

根据this 文章,我现在可以在Windows 窗体的Load 事件中加载集合。但我不想那样。

换句话说,报告不应加载自定义StoreCollection(自定义Store 类型的List<T>)中的所有行,而应仅加载由参数确定的行。

我将如何做到这一点?

注意:我知道BindingSource 有一个Filter 属性,但我不确定如何将我的参数传递给它(这些参数用于从数据库中检索数据和一个自定义列表返回类型)。

谢谢。

【问题讨论】:

  • 创建一个对象来包含这些值。使用 LINQ 填充这些对象的列表,并将 XtraReport 的数据源设置为对象列表的数据源。重建项目,然后在 xtrareport 中放置一个“绑定源”并将其源设置为数据对象(而不是列表),然后将“字段”拖放到报告上。如果这有用,将在早上发布代码

标签: c# devexpress reporting bindingsource


【解决方案1】:

我会在数据进入报告之前使用 LINQ 来选择数据。我的代码在 VB.net 中,但可以很容易地翻译:

1 - 创建一个数据对象 - 将包含我们的数据

Public Class Animal
    Public name As String
    Public livesYears As Integer
    Public location As String
End Class

2 - 创建 XtraReport1。将BindingSource 放到设计器上并将其设置为DataSourceAnimal。如果Animal 没有出现在向导生成的列表中,您将需要重新构建您的解决方案。将几个字段拖放到设计器上...“名称”等,以便报表有一些内容...报表!

3 - 创建 sub 以填充列表

Private Function createAnimals() As List(Of Animal)
    Dim allAnimals As New List(Of Animal)

    allAnimals.Add(New Animal With {.name = "Snake", .livesYears = "12", .location = "Africa"})
    allAnimals.Add(New Animal With {.name = "Dog", .livesYears = "17", .location = "England"})
    allAnimals.Add(New Animal With {.name = "Cat", .livesYears = "14", .location = "Egypt"})
    allAnimals.Add(New Animal With {.name = "Hedgehog", .livesYears = "4", .location = "England"})
    allAnimals.Add(New Animal With {.name = "Dragon", .livesYears = "350", .location = "Canada"})
    allAnimals.Add(New Animal With {.name = "Bat", .livesYears = "28", .location = "Scotland"})

    Return allAnimals
End Function

4 - 在表单加载中创建报表实例

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load

    'create our list of animals (could be a for loop that adds each dataset row to the list of Animal)
    Dim allAnimals As List(Of Animal) = createAnimals()

    'select just the Animals that we want
    Dim justTheAnimalsIWant = (From ani In allAnimals
                              Where ani.location = "England"
                              Select ani).ToList

    'create instance of the report
    Dim report As New XtraReport1

    'set the datasource to justTheAnimalsIWant
    report.DataSource = justTheAnimalsIWant

    Dim printTool As ReportPrintTool = New ReportPrintTool(report)
    printTool.ShowPreview()
End Sub

上面的示例没有使用数据集,它使用了我们的Animal 对象列表。要填充我们的 Animal 对象列表,您可以使用 for 循环遍历数据行并添加到 Animal 对象列表中。然后在使用 LINQ 选择您想要的内容后,就像使用 justTheAnimalsIWant 一样。简单。

【讨论】:

    猜你喜欢
    • 2022-01-11
    • 1970-01-01
    • 2020-07-25
    • 1970-01-01
    • 1970-01-01
    • 2021-07-28
    • 1970-01-01
    • 2018-11-24
    • 1970-01-01
    相关资源
    最近更新 更多