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

namespace DsWordWeb.Demos
{
    // This sample uses data templates to generate a table with rows
    // corresponding to oceans, and a nested table with seas.
    public class DataTplNestedTable
    {
        public GcWordDocument CreateDocx()
        {
            // The data source (ocean and sea data from Wikipedia):
            var oceans = new[]
            {
                new { name = "Pacific", areaOfWorldOcean = 0.466, volumeOfWorldOcean = 0.501, seas = new[]
                    { new {name = "Australasian Mediterranean Sea" }, new { name = "Philippine Sea" }, new { name = "Coral Sea" }, new { name = "South China Sea"}  }
                },
                new { name = "Atlantic", areaOfWorldOcean = 0.235, volumeOfWorldOcean = 0.233, seas = new[]
                    { new {name = "Sargasso Sea" }, new { name = "Caribbean Sea" }, new { name = "Mediterranean Sea" }, new { name = "Gulf of Guinea" } }
                },
                new { name = "Indian", areaOfWorldOcean = 0.195, volumeOfWorldOcean = 0.198, seas = new[]
                    { new {name = "Arabian Sea" }, new { name = "Bay of Bengal" }, new { name = "Andaman Sea" }, new { name = "Laccadive Sea" } }
                },
                new { name = "Southern", areaOfWorldOcean = 0.061, volumeOfWorldOcean = 0.054, seas = new[]
                    { new {name = "Weddell Sea" }, new { name = "Somov Sea" }, new { name = "Riiser-Larsen Sea" }, new { name = "Lazarev Sea" } }
                },
                new { name = "Arctic", areaOfWorldOcean = 0.043, volumeOfWorldOcean = 0.014, seas = new[]
                    { new {name = "Barents Sea" }, new { name = "Greenland Sea" }, new { name = "East Siberian Sea" }, new { name = "Kara Sea" } }
                },
            };

            var doc = new GcWordDocument();

            // Add the data source to the data template data sources:
            doc.DataTemplate.DataSources.Add("ds", oceans);

            // Add a table for oceans, and a nested table for seas:
            var t = doc.Body.Tables.Add(3, 1);
            t.Style = doc.Styles.Add("my table style", StyleType.Table);
            t.Style.BaseStyle = doc.Styles[BuiltInStyleId.ListTable5DarkAccent1];
            var tnested = t.Rows[0].Cells[2].GetRange().Tables.Add(1, 1);
            tnested.Style = doc.Styles.Add("my nested table style", StyleType.Table);
            tnested.Style.BaseStyle = t.Style.BaseStyle;
            tnested.Style.Font.Color.RGB = Color.PaleGoldenrod;
            tnested.Style.Font.Size -= 1;

            // Specify data bindings:
            t.Rows[0].Cells[0].GetRange().Paragraphs.First.GetRange().Runs.Add("{{#ds}}{{name}}");
            t.Rows[0].Cells[1].GetRange().Paragraphs.First.GetRange().Runs.Add("{{areaOfWorldOcean}:format(0.#%)}");
            tnested.Rows[0].Cells[0].GetRange().Paragraphs.First.GetRange().Runs.Add("{{seas.name}}");
            t.Rows[0].Cells[2].GetRange().Paragraphs.First.GetRange().Runs.Add("{{/ds}}");

            // This call expands all data templates in the document,
            // replacing template tags with data (iterating over all data items):
            doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"));

            // Done:
            return doc;
        }
    }
}