-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.vb
229 lines (176 loc) · 7.71 KB
/
Form1.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
Imports DevExpress.XtraReports.UI
Imports DevExpress.XtraPrinting
Imports DevExpress.XtraCharts
Namespace WindowsFormsApp1
Partial Public Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
' Create a report instance.
Dim report = New XtraReport()
' Set up the report's data source.
report.DataSource = CreateDataSet()
report.DataMember = (TryCast(report.DataSource, DataSet)).Tables(0).TableName
' Create bands.
CreateBands(report)
' Apply styles.
ApplyStyles(report)
' Add controls to the bands and bind the controls to data.
If xrTableRadioButton.Checked Then
' Use the XRTable control to display data.
InitializeBandsUsingXRTable(report)
Else
' Use the XRLabel control to display data.
InitializeBandsUsingXRLabel(report)
End If
' Create a chart, bind it to data, and add the chart to the report.
InitializeChart(report)
' Show the report's Preview.
report.ShowPreviewDialog()
End Sub
Public Function CreateDataSet() As DataSet
Dim dataSet = New DataSet() With {.DataSetName = "Categories"}
Dim dataTable = New DataTable() With {.TableName = "Category"}
dataSet.Tables.Add(dataTable)
dataTable.Columns.Add("Category Name", GetType(String))
dataTable.Columns.Add("Amount 1", GetType(Integer))
dataTable.Columns.Add("Amount 2", GetType(Integer))
dataTable.Columns.Add("Amount 3", GetType(Integer))
dataSet.Tables("Category").Rows.Add(New Object() { "Beverage", 10, 30, 2 })
dataSet.Tables("Category").Rows.Add(New Object() { "Groceries", 20, 40, 4 })
dataSet.Tables("Category").Rows.Add(New Object() { "Meat", 30, 50, 1 })
dataSet.Tables("Category").Rows.Add(New Object() { "Vegetable", 5, 5, 8 })
dataSet.Tables("Category").Rows.Add(New Object() { "Fish", 5, 5, 10 })
Return dataSet
End Function
Public Sub CreateBands(ByVal report As XtraReport)
Dim detail = New DetailBand() With {.HeightF = 20}
Dim pageHeader = New PageHeaderBand() With {.HeightF = 20}
Dim reportFooter = New ReportFooterBand() With {.HeightF = 380}
report.Bands.AddRange(New Band() { detail, pageHeader, reportFooter })
End Sub
Public Sub ApplyStyles(ByVal report As XtraReport)
' Create odd and even styles and set up their appearance.
Dim oddStyle = New XRControlStyle()
Dim evenStyle = New XRControlStyle()
oddStyle.BackColor = Color.LightBlue
oddStyle.StyleUsing.UseBackColor = True
oddStyle.StyleUsing.UseBorders = False
oddStyle.Name = "OddStyle"
evenStyle.BackColor = Color.LightPink
evenStyle.StyleUsing.UseBackColor = True
evenStyle.StyleUsing.UseBorders = False
evenStyle.Name = "EvenStyle"
' Add the styles to the report's StyleSheet collection.
report.StyleSheet.AddRange(New XRControlStyle() { oddStyle, evenStyle })
End Sub
Public Sub InitializeBandsUsingXRLabel(ByVal report As XtraReport)
Dim ds = TryCast(report.DataSource, DataSet)
Dim colCount As Integer = ds.Tables(0).Columns.Count
Dim colWidth As Integer = (report.PageWidth - (report.Margins.Left + report.Margins.Right)) / colCount
' Create header captions.
For i As Integer = 0 To colCount - 1
Dim label = New XRLabel()
label.Location = New Point(colWidth * i, 0)
label.Size = New Size(colWidth, 20)
label.Text = ds.Tables(0).Columns(i).Caption
If i > 0 Then
label.Borders = BorderSide.Right Or BorderSide.Top Or BorderSide.Bottom
Else
label.Borders = BorderSide.All
End If
' Add a label with the caption to the report's PageHeader band.
report.Bands(BandKind.PageHeader).Controls.Add(label)
Next i
' Create data-bound labels with different odd and even backgrounds.
For i As Integer = 0 To colCount - 1
Dim label = New XRLabel()
label.Location = New Point(colWidth * i, 0)
label.Size = New Size(colWidth, 20)
label.ExpressionBindings.Add(New ExpressionBinding("Text", "[" & ds.Tables(0).Columns(i).Caption & "]"))
label.OddStyleName = "OddStyle"
label.EvenStyleName = "EvenStyle"
If i > 0 Then
label.Borders = BorderSide.Right Or BorderSide.Bottom
Else
label.Borders = BorderSide.Left Or BorderSide.Right Or BorderSide.Bottom
End If
' Add the label to the report's Detail band.
report.Bands(BandKind.Detail).Controls.Add(label)
Next i
End Sub
Public Sub InitializeBandsUsingXRTable(ByVal report As XtraReport)
Dim ds = (TryCast(report.DataSource, DataSet))
Dim colCount As Integer = ds.Tables(0).Columns.Count
Dim colWidth As Integer = (report.PageWidth - (report.Margins.Left + report.Margins.Right)) / colCount
' Create a table header.
Dim tableHeader = New XRTable()
tableHeader.Height = 20
tableHeader.Width = (report.PageWidth - (report.Margins.Left + report.Margins.Right))
Dim headerRow = New XRTableRow()
headerRow.Width = tableHeader.Width
tableHeader.Rows.Add(headerRow)
tableHeader.BeginInit()
' Create a table body.
Dim tableBody = New XRTable()
tableBody.Height = 20
tableBody.Width = (report.PageWidth - (report.Margins.Left + report.Margins.Right))
Dim bodyRow = New XRTableRow()
bodyRow.Width = tableBody.Width
tableBody.Rows.Add(bodyRow)
tableBody.EvenStyleName = "EvenStyle"
tableBody.OddStyleName = "OddStyle"
tableBody.BeginInit()
' Initialize table header and body cells.
For i As Integer = 0 To colCount - 1
Dim headerCell = New XRTableCell()
headerCell.Width = colWidth
headerCell.Text = ds.Tables(0).Columns(i).Caption
Dim bodyCell = New XRTableCell()
bodyCell.Width = colWidth
bodyCell.DataBindings.Add("Text", Nothing, ds.Tables(0).Columns(i).Caption)
If i = 0 Then
headerCell.Borders = BorderSide.Left Or BorderSide.Top Or BorderSide.Bottom
bodyCell.Borders = BorderSide.Left Or BorderSide.Top Or BorderSide.Bottom
Else
headerCell.Borders = BorderSide.All
bodyCell.Borders = BorderSide.All
End If
headerRow.Cells.Add(headerCell)
bodyRow.Cells.Add(bodyCell)
Next i
tableHeader.EndInit()
tableBody.EndInit()
' Add the table header and body to the corresponding report bands.
report.Bands(BandKind.PageHeader).Controls.Add(tableHeader)
report.Bands(BandKind.Detail).Controls.Add(tableBody)
End Sub
Public Sub InitializeChart(ByVal report As XtraReport)
Dim ds = TryCast(report.DataSource, DataSet)
' Create a chart.
Dim chart = New XRChart()
chart.Location = New Point(0, 0)
chart.Name = "xrChart1"
' Create chart series and bind them to data.
For i As Integer = 1 To (ds.Tables(0).Columns.Count) - 1
If ds.Tables(0).Columns(i).DataType Is GetType(Integer) OrElse ds.Tables(0).Columns(i).DataType Is GetType(Double) Then
Dim series = New Series(ds.Tables(0).Columns(i).Caption, ViewType.Bar)
series.DataSource = ds.Tables(0)
series.ArgumentDataMember = ds.Tables(0).Columns(0).Caption
series.ValueDataMembers(0) = ds.Tables(0).Columns(i).Caption
chart.Series.Add(series)
End If
Next i
' Customize the chart appearance.
TryCast(chart.Diagram, XYDiagram).AxisX.Label.Angle = 20
TryCast(chart.Diagram, XYDiagram).AxisX.Label.Font = New Font("Tahoma", 9.75F, FontStyle.Regular, GraphicsUnit.Point, (CByte(204)))
chart.Size = New Size(650, 360)
' Add the chart to the report's footer.
report.Bands(BandKind.ReportFooter).Controls.Add(chart)
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
End Sub
End Class
End Namespace