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

'' This data template sample loads a DOCX that was created in MS Word,
'' And contains a table with data template tags. It then creates a DataSet,
'' loads the GcNwind.xml data base into it, And builds a product list
'' from tables in that data set. The product list Is set as the template
'' data source on the document, And the template Is processed to build
'' the final document with actual data.
Public Class DataTplProductList
    Public Function CreateDocx(ByRef sampleParams As String()) As GcWordDocument
        Dim doc = New GcWordDocument()

        '' Load the template DOCX
        doc.Load(Path.Combine("Resources", "WordDocs", sampleParams(3)))

        Using ds = New DataSet()
            '' Load the data And build the product list data source:
            ds.ReadXml(Path.Combine("Resources", "data", "GcNWind.xml"))

            Dim dtProds As DataTable = ds.Tables("Products")
            Dim dtSupps As DataTable = ds.Tables("Suppliers")

            Dim products =
                From prod In dtProds.Select()
                Join supp In dtSupps.Select()
                On prod("SupplierID") Equals supp("SupplierID")
                Order By prod("ProductName")
                Select New With
                {
                    .ProductID = prod("ProductID"),
                    .ProductName = prod("ProductName"),
                    .Supplier = supp("CompanyName"),
                    .QuantityPerUnit = prod("QuantityPerUnit"),
                    .UnitPrice = prod("UnitPrice")
                }

            '' Add the data source to the data template data sources
            '' (note that in this release, only one data source can be added):
            doc.DataTemplate.DataSources.Add("ds", products)

            '' The document already has all the necessary bindings,
            '' so we only need to process the data template:
            doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"))
        End Using

        '' Done
        Return doc
    End Function

    Public Function CreateDocx(Optional parsIdx As Integer = 0) As GcWordDocument
        Return CreateDocx(GetSampleParamsList()(parsIdx))
    End Function

    Public Shared Function GetSampleParamsList() As List(Of String())
        '' Strings are name, description, info, template docx
        Return New List(Of String()) From
            {
                New String() {"@data-templates/Product List",
                    "Load a template DOCX and bind it to a product list from an XML data set", Nothing,
                    "ProductListTemplate.docx"},
                New String() {"@data-templates/Formatter Chain",
                    "Load a template that uses chained formatters to highlight products that cost $50+", Nothing,
                    "ProductListTemplate-cond.docx"}
            }
    End Function
End Class