DataTplCollState.vb
''
'' This code is part of Document Solutions for Word demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.Linq
Imports System.Globalization
Imports GrapeCity.Documents.Word

'' This example demonstrates the use of collection state functions
'' Index(), IsFirst() and IsLast() that can be used with the report templates
'' 'calc' feature.
Public Class DataTplCollState
    Function CreateDocx() As GcWordDocument
        '' The original data source (ocean and sea data from Wikipedia):
        Dim oceans =
        {
            New With {.name = "Pacific", .areaOfWorldOcean = 0.466, .volumeOfWorldOcean = 0.501, .seas =
                {New With {.name = "Australasian Mediterranean Sea"}, New With {.name = "Philippine Sea"}, New With {.name = "Coral Sea"}, New With {.name = "South China Sea"}}},
            New With {.name = "Atlantic", .areaOfWorldOcean = 0.235, .volumeOfWorldOcean = 0.233, .seas =
                {New With {.name = "Sargasso Sea"}, New With {.name = "Caribbean Sea"}, New With {.name = "Mediterranean Sea"}, New With {.name = "Gulf of Guinea"}}},
            New With {.name = "Indian", .areaOfWorldOcean = 0.195, .volumeOfWorldOcean = 0.198, .seas =
                {New With {.name = "Arabian Sea"}, New With {.name = "Bay of Bengal"}, New With {.name = "Andaman Sea"}, New With {.name = "Laccadive Sea"}}},
            New With {.name = "Southern", .areaOfWorldOcean = 0.061, .volumeOfWorldOcean = 0.054, .seas =
                {New With {.name = "Weddell Sea"}, New With {.name = "Somov Sea"}, New With {.name = "Riiser-Larsen Sea"}, New With {.name = "Lazarev Sea"}}},
            New With {.name = "Arctic", .areaOfWorldOcean = 0.043, .volumeOfWorldOcean = 0.014, .seas =
                {New With {.name = "Barents Sea"}, New With {.name = "Greenland Sea"}, New With {.name = "East Siberian Sea"}, New With {.name = "Kara Sea"}}}
        }
        '' Flatten the data source to get a flat collection of seas:
        Dim seas = oceans.SelectMany(Function(o) o.seas)

        '' Create the document and add the sea list as the data source:
        Dim doc = New GcWordDocument()
        doc.DataTemplate.DataSources.Add("ds", seas)

        '' Template that for each record (sea) prints:
        '' - The 1-based number of the collection element (sea) and the total number of records;
        '' - The sea name;
        '' - The string '(first)' after the first element (sea);
        '' - The string '(last)' after the last element (sea):
        Dim p = doc.Body.Paragraphs.Add(
            "Record {{calc Index(ds)+1}} of {{calc Count(ds)}}: {{ds.name}}{{if IsFirst(ds)}} (first){{else}}{{if IsLast(ds)}} (last){{endif}}{{endif}}")

        '' Expand the template:
        doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"))

        '' Done:
        Return doc
    End Function
End Class