DevExpress Report必须在数据源级别使用Parameters过滤BindingSource

时间:2013-08-01 13:04:45

标签: c# devexpress reporting bindingsource

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

我已经在报表设计器中声明了参数和bindingsource。所以我有领域和所有设置。

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

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

我该如何做到这一点?

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

谢谢。

1 个答案:

答案 0 :(得分:0)

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

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

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

2 - 创建XtraReport1。将BindingSource放到设计器上并将其DataSource设置为Animal。如果Animal未显示在向导生成的列表中,则需要重建解决方案。将几个字段放到设计器上......“名称”等,以便报告将有...报告!

3 - 创建子以填充列表

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一样。 Simples。