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

'' This example demonstrates the use of 'if/else' construct in report templates
'' to filter data source on a condition (that a certain field starts with the letter 'S').
Public Class DataTplIfStartsWith
    Function CreateDocx() As GcWordDocument
        '' The 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"}}}
        }

        Dim doc = New GcWordDocument()

        '' Add the data source:
        doc.DataTemplate.DataSources.Add("ds", oceans)

        '' Add a list template so that the data is formatted as a list:
        Dim myListTemplate = doc.ListTemplates.Add(BuiltInListTemplateId.BulletDefault, "myListTemplate")

        '' Filter output using if/then/else - print only names starting with an 's':
        Dim p = doc.Body.Paragraphs.Add("{{if StartsWith(UCase(ds.seas.name),""S"")}}{{ds.seas.name}}{{endif}}", doc.Styles(BuiltInStyleId.ListParagraph))
        p.ListFormat.Template = myListTemplate

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

        '' Add a short note describing the demo at the top of the document:
        doc.Body.Paragraphs.Insert(
            "This example demonstrates the use of 'if/else' construct in report templates " +
            "to filter data source on a condition (that a certain field starts with the letter 'S')." +
            "Please see this sample source code for full details.",
            InsertLocation.Start)
        doc.Body.Paragraphs.Insert("Report templates: if data starts with 'S'", doc.Styles(BuiltInStyleId.Heading1), InsertLocation.Start)

        '' Done:
        Return doc
    End Function
End Class