DataTplProductList.cs
//
// This code is part of Document Solutions for Word samples.
// Copyright (c) MESCIUS inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Linq;
using GrapeCity.Documents.Word;

namespace DsWordWeb.Demos
{
    // 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 GcWordDocument CreateDocx(string[] sampleParams)
        {
            var doc = new GcWordDocument();

            // Load the template DOCX:
            doc.Load(Path.Combine("Resources", "WordDocs", sampleParams[3]));

            using (var ds = new DataSet())
            {
                // Load the data and build the product list data source:
                ds.ReadXml(Path.Combine("Resources", "data", "GcNWind.xml"));

                DataTable dtProds = ds.Tables["Products"];
                DataTable dtSupps = ds.Tables["Suppliers"];

                var products =
                    from prod in dtProds.Select()
                    join supp in dtSupps.Select()
                    on prod["SupplierID"] equals supp["SupplierID"]
                    orderby prod["ProductName"]
                    select new
                    {
                        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:
                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();
            }

            // Done:
            return doc;
        }

        public GcWordDocument CreateDocx(int paramIdx = 0)
        {
            return CreateDocx(GetSampleParamsList()[paramIdx]);
        }

        public static List<string[]> GetSampleParamsList()
        {
            // Strings are name, description, info, template docx:
            return new List<string[]>()
            {
                new string[] { "@data-templates/Product List",
                    "Load a template DOCX and bind it to a product list from an XML data set", null,
                    "ProductListTemplate.docx" },
                new string[] { "@data-templates/Formatter Chain",
                    "Load a template that uses chained formatters to highlight products that cost $50+", null,
                    "ProductListTemplate-cond.docx" },
            };
        }
    }
}