DataTplCollState.cs
C# VB
//// This code is part of Document Solutions for Word demos.// Copyright (c) MESCIUS inc. All rights reserved.//using System.Globalization;using System.Linq;using GrapeCity.Documents.Word;
namespace DsWordWeb.Demos{ // 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 { public GcWordDocument CreateDocx() { // The original 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" } } }, }; // Flatten the data source to get a flat collection of seas: var seas = oceans.SelectMany(o => o.seas);
// Create the document and add the sea list as the data source: var 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): var 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; } }}