Back to Devexpress

BandCollection Class

xtrareports-devexpress-dot-xtrareports-dot-ui-4393c168.md

latest6.7 KB
Original Source

BandCollection Class

A collection of Band objects.

Namespace : DevExpress.XtraReports.UI

Assembly : DevExpress.XtraReports.v25.2.dll

NuGet Package : DevExpress.Reporting.Core

Declaration

csharp
public class BandCollection :
    XRControlCollection,
    IEnumerable<Band>,
    IEnumerable
vb
Public Class BandCollection
    Inherits XRControlCollection
    Implements IEnumerable(Of Band),
               IEnumerable

The following members return BandCollection objects:

Remarks

When you create and edit a report, use Add, AddRange, and Remove methods to add new bands to the report or remove existing bands from the collection.

The Contains method allows you to determine if the collection already contains the specified band.

Note that the Add and AddRange methods are not intended to copy bands from one report to another. You should create a new instance of the Band, and then add it to the BandCollection.

Example

The following example demonstrates how to use the BandCollection class methods to construct a simple report. The AddMarginBands method creates two margin bands and adds them to the collection.

csharp
using DevExpress.XtraPrinting;
using DevExpress.XtraReports.UI;
// ...

public class XtraReport1 : DevExpress.XtraReports.UI.XtraReport {
// ...

    // ...
    // Generated code for the XtraReport1 class.
    // ...

    public void AddMarginBands() {

        // Check if the TopMargin band is already present in the report.
        if(Bands[BandKind.TopMargin] == null) {
            // Create a new TopMargin band and add it to the report.
            TopMarginBand tmBand = new TopMarginBand();
            Bands.Add(tmBand);

            // Create a label and set its text and width.
            XRLabel label = new XRLabel();
            label.Text = "TopMargin Band";
            label.Width = 200;

            // Add the label to the TopMargin band.
            tmBand.Controls.Add(label);
        }

        // Check if the BottomMargin band is already present in the report.
        if(Bands[BandKind.BottomMargin] == null) {
            // Create a new BottomMargin band and add it to the report.
            BottomMarginBand bmBand = new BottomMarginBand();
            Bands.Add(bmBand);

            // Create an XRPageInfo object and set its width and PageInfo option.
            XRPageInfo datePageInfo = new XRPageInfo();
            datePageInfo.Width = 200;
            datePageInfo.PageInfo = PageInfo.DateTime;

            // Add the page information control to the BottomMargin band.
            bmBand.Controls.Add(datePageInfo);
        }
    }

}
vb
Imports DevExpress.XtraPrinting
Imports DevExpress.XtraReports.UI
' ...

Public Class XtraReport1
    Inherits DevExpress.XtraReports.UI.XtraReport
    ' ...

    ' ...
    ' Generated code for the XtraReport1 class.
    ' ...

    Public Sub AddMarginBands()

        ' Check if the TopMargin band is already present in the report.
        If Bands.GetBandByType(GetType(TopMarginBand)) Is Nothing Then
            ' Create a new TopMargin band and add it to the report.
            Dim tmBand As New TopMarginBand()
            Bands.Add(tmBand)

            ' Create a label and set its text and width.
            Dim label As New XRLabel()
            label.Text = "TopMargin Band"
            label.Width = 200

            ' Add the label to the TopMargin band.
            tmBand.Controls.Add(label)
        End If

        ' Check if the BottomMargin band is already present in the report.
        If Bands.GetBandByType(GetType(BottomMarginBand)) Is Nothing Then
            ' Create a new BottomMargin band and add it to the report.
            Dim bmBand As New BottomMarginBand()
            Bands.Add(bmBand)

            ' Create an XRPageInfo object and set its width and PageInfo option.
            Dim datePageInfo As New XRPageInfo()
            datePageInfo.Width = 200
            datePageInfo.PageInfo = PageInfo.DateTime

            ' Add the page information control to the BottomMargin band.
            bmBand.Controls.Add(datePageInfo)
        End If
    End Sub

End Class

The following code handles button clicks to call the AddMarginBands method and show the preview:

csharp
XtraReport1 report = new XtraReport1();

private void btnPreview_Click(object sender, System.EventArgs e) {
    ReportPrintTool preview = new ReportPrintTool(report);
    preview.ShowPreview();
}

private void btnAddBands_Click(object sender, System.EventArgs e) {
    report.AddMarginBands();
}
vb
Dim Report As New XtraReport1()

Private Sub BtnPreview_Click(sender As Object, e As System.EventArgs) _
Handles BtnPreview.Click
    Dim preview As New ReportPrintTool(report)
    preview.ShowPreview()
End Sub

Private Sub BtnAddBands_Click(sender As Object, e As System.EventArgs) _
Handles BtnAddBands.Click
    Report.AddMarginBands()
End Sub

Inheritance

Object CollectionBase XRControlCollectionBase XRControlCollection BandCollection DetailReportBandCollection

See Also

BandCollection Members

DevExpress.XtraReports.UI Namespace