سؤال

I am trying to bind a XtraReport to a BindingSource (instead of a Dataset) and want to filter the values in the datasource before they get to the report using report parameters.

I have already declared the parameters and the bindingsource in the report designer. So I have the fields and everything set.

According to this article I can now load the collection in the Load event of a Windows form. But I don't want that.

In other words, the report should not load all rows from the custom StoreCollection (a List<T> of custom Store types), but only those determined by the parameters.

How would I do accomplish this?

Note: I know the BindingSource has a Filter property, but I am not sure how I would pass my parameters to it (the parameters are used to retrieve data form the database and a List of custom types is returned).

Thank you.

هل كانت مفيدة؟

المحلول

I would use LINQ to select the data before it goes into the report. My code is in VB.net but can fairly easily be translated:

1 - Create a data object - that will contain our data

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

2 - Create the XtraReport1. Drop a BindingSource onto the designer and set it's DataSource to Animal. If Animal doesn't show up in the list that the wizard generates, you will need to rebuild your solution. Drop a couple of the Fields onto the designer... 'name' etc so that the report will have something to... report!

3 - Create sub to populate the list

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 - Create an instance of the report in the Form Load

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

The above example doesn't use a dataset, it uses a list of our Animal objects. To populate our list of Animal objects you could use a for loop to iterate through the data-rows and add to the list of Animal objects. Then after use LINQ to select what you want, just like with the justTheAnimalsIWant. Simples.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top