FastReport

Форк
0
/
GroupHeaderBand.cs 
457 строк · 14.1 Кб
1
using System.ComponentModel;
2
using FastReport.Utils;
3
using FastReport.Data;
4
using System.Drawing.Design;
5

6
namespace FastReport
7
{
8
    /// <summary>
9
    /// Specifies a sort order.
10
    /// </summary>
11
    /// <remarks>
12
    /// This enumeration is used in the group header and in the "Matrix" object.
13
    /// </remarks>
14
    public enum SortOrder
15
    {
16
        /// <summary>
17
        /// Specifies no sort (natural order).
18
        /// </summary>
19
        None,
20

21
        /// <summary>
22
        /// Specifies an ascending sort order.
23
        /// </summary>
24
        Ascending,
25

26
        /// <summary>
27
        /// Specifies a descending sort order.
28
        /// </summary>
29
        Descending
30
    }
31

32
    /// <summary>
33
    /// Represents a group header band.
34
    /// </summary>
35
    /// <remarks>
36
    /// A simple group consists of one <b>GroupHeaderBand</b> and the <b>DataBand</b> that is set 
37
    /// to the <see cref="Data"/> property. To create the nested groups, use the <see cref="NestedGroup"/> property.
38
    /// <note type="caution">
39
    /// Only the last nested group can have data band.
40
    /// </note>
41
    /// <para/>Use the <see cref="Condition"/> property to set the group condition. The <see cref="SortOrder"/>
42
    /// property can be used to set the sort order for group's data rows. You can also use the <b>Sort</b>
43
    /// property of the group's <b>DataBand</b> to specify additional sort.
44
    /// </remarks>
45
    /// <example>This example shows how to create nested groups.
46
    /// <code>
47
    /// ReportPage page = report.Pages[0] as ReportPage;
48
    /// 
49
    /// // create the main group
50
    /// GroupHeaderBand mainGroup = new GroupHeaderBand();
51
    /// mainGroup.Height = Units.Millimeters * 10;
52
    /// mainGroup.Name = "MainGroup";
53
    /// mainGroup.Condition = "[Orders.CustomerName]";
54
    /// // add a group to the page
55
    /// page.Bands.Add(mainGroup);
56
    /// 
57
    /// // create the nested group
58
    /// GroupHeaderBand nestedGroup = new GroupHeaderBand();
59
    /// nestedGroup.Height = Units.Millimeters * 10;
60
    /// nestedGroup.Name = "NestedGroup";
61
    /// nestedGroup.Condition = "[Orders.OrderDate]";
62
    /// // add it to the main group
63
    /// mainGroup.NestedGroup = nestedGroup;
64
    /// 
65
    /// // create a data band
66
    /// DataBand dataBand = new DataBand();
67
    /// dataBand.Height = Units.Millimeters * 10;
68
    /// dataBand.Name = "GroupData";
69
    /// dataBand.DataSource = report.GetDataSource("Orders");
70
    /// // connect the databand to the nested group
71
    /// nestedGroup.Data = dataBand;
72
    /// </code>
73
    /// </example>
74
    public partial class GroupHeaderBand : HeaderFooterBandBase
75
    {
76
        #region Fields
77
        private GroupHeaderBand nestedGroup;
78
        private DataBand data;
79
        private GroupFooterBand groupFooter;
80
        private DataHeaderBand header;
81
        private DataFooterBand footer;
82
        private string condition;
83
        private SortOrder sortOrder;
84
        private bool keepTogether;
85
        private bool resetPageNumber;
86
        private object groupValue;
87
        #endregion
88

89
        #region Properties
90
        /// <summary>
91
        /// Gets or sets a nested group.
92
        /// </summary>
93
        /// <remarks>
94
        /// Use this property to create nested groups.
95
        /// <note type="caution">
96
        /// Only the last nested group can have data band.
97
        /// </note>
98
        /// </remarks>
99
        /// <example>
100
        /// This example demonstrates how to create a group with nested group.
101
        /// <code>
102
        /// ReportPage page;
103
        /// GroupHeaderBand group = new GroupHeaderBand();
104
        /// group.NestedGroup = new GroupHeaderBand();
105
        /// group.NestedGroup.Data = new DataBand();
106
        /// page.Bands.Add(group);
107
        /// </code>
108
        /// </example>
109
        [Browsable(false)]
110
        public GroupHeaderBand NestedGroup
111
        {
112
            get { return nestedGroup; }
113
            set
114
            {
115
                SetProp(nestedGroup, value);
116
                nestedGroup = value;
117
            }
118
        }
119

120
        /// <summary>
121
        /// Gets or sets the group data band.
122
        /// </summary>
123
        /// <remarks>
124
        /// Use this property to add a data band to a group. Note: only the last nested group can have Data band.
125
        /// </remarks>
126
        /// <example>
127
        /// This example demonstrates how to add a data band to a group.
128
        /// <code>
129
        /// ReportPage page;
130
        /// GroupHeaderBand group = new GroupHeaderBand();
131
        /// group.Data = new DataBand();
132
        /// page.Bands.Add(group);
133
        /// </code>
134
        /// </example>
135
        [Browsable(false)]
136
        public DataBand Data
137
        {
138
            get { return data; }
139
            set
140
            {
141
                SetProp(data, value);
142
                data = value;
143
            }
144
        }
145

146
        /// <summary>
147
        /// Gets or sets a group footer.
148
        /// </summary>
149
        [Browsable(false)]
150
        public GroupFooterBand GroupFooter
151
        {
152
            get { return groupFooter; }
153
            set
154
            {
155
                SetProp(groupFooter, value);
156
                groupFooter = value;
157
            }
158
        }
159

160
        /// <summary>
161
        /// Gets or sets a header band.
162
        /// </summary>
163
        [Browsable(false)]
164
        public DataHeaderBand Header
165
        {
166
            get { return header; }
167
            set
168
            {
169
                SetProp(header, value);
170
                header = value;
171
            }
172
        }
173

174
        /// <summary>
175
        /// Gets or sets a footer band.
176
        /// </summary>
177
        /// <remarks>
178
        /// To access a group footer band, use the <see cref="GroupFooter"/> property.
179
        /// </remarks>
180
        [Browsable(false)]
181
        public DataFooterBand Footer
182
        {
183
            get { return footer; }
184
            set
185
            {
186
                SetProp(footer, value);
187
                footer = value;
188
            }
189
        }
190

191
        /// <summary>
192
        /// Gets or sets the group condition.
193
        /// </summary>
194
        /// <remarks>
195
        /// This property can contain any valid expression. When running a report, this expression is calculated 
196
        /// for each data row. When the value of this condition is changed, FastReport starts a new group.
197
        /// </remarks>
198
        [Category("Data")]
199
        [Editor("FastReport.TypeEditors.ExpressionEditor, FastReport", typeof(UITypeEditor))]
200
        public string Condition
201
        {
202
            get { return condition; }
203
            set { condition = value; }
204
        }
205

206
        /// <summary>
207
        /// Gets or sets the sort order.
208
        /// </summary>
209
        /// <remarks>
210
        /// FastReport can sort data rows automatically using the <see cref="Condition"/> value.
211
        /// </remarks>
212
        [DefaultValue(SortOrder.Ascending)]
213
        [Category("Behavior")]
214
        public SortOrder SortOrder
215
        {
216
            get { return sortOrder; }
217
            set { sortOrder = value; }
218
        }
219

220
        /// <summary>
221
        /// Gets or sets a value indicating that the group should be printed together on one page.
222
        /// </summary>
223
        [DefaultValue(false)]
224
        [Category("Behavior")]
225
        public bool KeepTogether
226
        {
227
            get { return keepTogether; }
228
            set { keepTogether = value; }
229
        }
230

231
        /// <summary>
232
        /// Gets or sets a value that determines whether to reset the page numbers when this group starts print.
233
        /// </summary>
234
        /// <remarks>
235
        /// Typically you should set the <see cref="BandBase.StartNewPage"/> property to <b>true</b> as well.
236
        /// </remarks>
237
        [DefaultValue(false)]
238
        [Category("Behavior")]
239
        public bool ResetPageNumber
240
        {
241
            get { return resetPageNumber; }
242
            set { resetPageNumber = value; }
243
        }
244

245
        internal DataSourceBase DataSource
246
        {
247
            get
248
            {
249
                DataBand dataBand = GroupDataBand;
250
                return dataBand == null ? null : dataBand.DataSource;
251
            }
252
        }
253

254
        internal DataBand GroupDataBand
255
        {
256
            get
257
            {
258
                GroupHeaderBand group = this;
259
                while (group != null)
260
                {
261
                    if (group.Data != null)
262
                        return group.Data;
263
                    group = group.NestedGroup;
264
                }
265
                return null;
266
            }
267
        }
268
        #endregion
269

270
        #region IParent
271
        /// <inheritdoc/>
272
        public override void GetChildObjects(ObjectCollection list)
273
        {
274
            base.GetChildObjects(list);
275
            if (!IsRunning)
276
            {
277
                list.Add(header);
278
                list.Add(nestedGroup);
279
                list.Add(data);
280
                list.Add(groupFooter);
281
                list.Add(footer);
282
            }
283
        }
284

285
        /// <inheritdoc/>
286
        public override bool CanContain(Base child)
287
        {
288
            return base.CanContain(child) ||
289
              (child is DataBand && nestedGroup == null && data == null) ||
290
              (child is GroupHeaderBand && (nestedGroup == null || nestedGroup is GroupHeaderBand) && data == null) ||
291
              child is GroupFooterBand || child is DataHeaderBand || child is DataFooterBand;
292
        }
293

294
        /// <inheritdoc/>
295
        public override void AddChild(Base child)
296
        {
297
            if (IsRunning)
298
            {
299
                base.AddChild(child);
300
                return;
301
            }
302

303
            if (child is GroupHeaderBand)
304
                NestedGroup = child as GroupHeaderBand;
305
            else if (child is DataBand)
306
                Data = child as DataBand;
307
            else if (child is GroupFooterBand)
308
                GroupFooter = child as GroupFooterBand;
309
            else if (child is DataHeaderBand)
310
                Header = child as DataHeaderBand;
311
            else if (child is DataFooterBand)
312
                Footer = child as DataFooterBand;
313
            else
314
                base.AddChild(child);
315
        }
316

317
        /// <inheritdoc/>
318
        public override void RemoveChild(Base child)
319
        {
320
            base.RemoveChild(child);
321
            if (IsRunning)
322
                return;
323

324
            if (child is GroupHeaderBand && nestedGroup == child)
325
                NestedGroup = null;
326
            if (child is DataBand && data == child as DataBand)
327
                Data = null;
328
            if (child is GroupFooterBand && groupFooter == child)
329
                GroupFooter = null;
330
            if (child is DataHeaderBand && header == child)
331
                Header = null;
332
            if (child is DataFooterBand && footer == child)
333
                Footer = null;
334
        }
335
        #endregion
336

337
        #region Public Methods
338
        /// <inheritdoc/>
339
        public override void Assign(Base source)
340
        {
341
            base.Assign(source);
342

343
            GroupHeaderBand src = source as GroupHeaderBand;
344
            Condition = src.Condition;
345
            SortOrder = src.SortOrder;
346
            KeepTogether = src.KeepTogether;
347
            ResetPageNumber = src.ResetPageNumber;
348
        }
349

350
        /// <inheritdoc/>
351
        public override void Serialize(FRWriter writer)
352
        {
353
            GroupHeaderBand c = writer.DiffObject as GroupHeaderBand;
354
            base.Serialize(writer);
355
            if (writer.SerializeTo == SerializeTo.Preview)
356
                return;
357

358
            if (Condition != c.Condition)
359
                writer.WriteStr("Condition", Condition);
360
            if (SortOrder != c.SortOrder)
361
                writer.WriteValue("SortOrder", SortOrder);
362
            if (KeepTogether != c.KeepTogether)
363
                writer.WriteBool("KeepTogether", KeepTogether);
364
            if (ResetPageNumber != c.ResetPageNumber)
365
                writer.WriteBool("ResetPageNumber", ResetPageNumber);
366
        }
367

368
        /// <inheritdoc/>
369
        public override string[] GetExpressions()
370
        {
371
            return new string[] { Condition };
372
        }
373

374
        internal override bool IsEmpty()
375
        {
376
            if (NestedGroup != null)
377
                return NestedGroup.IsEmpty();
378
            else if (Data != null)
379
                return Data.IsEmpty();
380
            return base.IsEmpty();
381
        }
382

383
        internal void InitDataSource()
384
        {
385
            DataBand dataBand = GroupDataBand;
386
            GroupHeaderBand group = this;
387
            int index = 0;
388
            // insert group sort to the databand
389
            while (group != null)
390
            {
391
                if (group.SortOrder != SortOrder.None)
392
                {
393
                    dataBand.Sort.Insert(index, new Sort(group.Condition, group.SortOrder == SortOrder.Descending));
394
                    index++;
395
                }
396
                group = group.NestedGroup;
397
            }
398

399
            dataBand.InitDataSource();
400
        }
401

402
        internal void FinalizeDataSource()
403
        {
404
            DataBand dataBand = GroupDataBand;
405
            GroupHeaderBand group = this;
406
            // remove group sort from the databand
407
            while (group != null)
408
            {
409
                if (group.SortOrder != SortOrder.None)
410
                    dataBand.Sort.RemoveAt(0);
411
                group = group.NestedGroup;
412
            }
413
        }
414

415
        internal void ResetGroupValue()
416
        {
417
            if (!string.IsNullOrEmpty(Condition))
418
            {
419
                groupValue = Report.Calc(Condition);
420
            }
421
            else
422
            {
423
                throw new GroupHeaderHasNoGroupCondition(Name);
424
            }
425
        }
426

427
        internal bool GroupValueChanged()
428
        {
429
            object value = null;
430
            if (!string.IsNullOrEmpty(Condition))
431
            {
432
                value = Report.Calc(Condition);
433
            }
434
            else
435
            {
436
                throw new GroupHeaderHasNoGroupCondition(Name);
437
            }
438
            if (groupValue == null)
439
            {
440
                if (value == null)
441
                    return false;
442
                return true;
443
            }
444
            return !groupValue.Equals(value);
445
        }
446
        #endregion
447

448
        /// <summary>
449
        /// Initializes a new instance of the <see cref="GroupHeaderBand"/> class with default settings.
450
        /// </summary>
451
        public GroupHeaderBand()
452
        {
453
            condition = "";
454
            sortOrder = SortOrder.Ascending;
455
        }
456
    }
457
}

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.