Я бы использовал 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
. Простые.
person
Chris
schedule
02.08.2013