DataTplProductList.cs
  1. //
  2. // This code is part of Document Solutions for Word demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. using System.Data;
  11. using System.Linq;
  12. using System.Globalization;
  13. using GrapeCity.Documents.Word;
  14.  
  15. namespace DsWordWeb.Demos
  16. {
  17. // This data template sample loads a DOCX that was created in MS Word,
  18. // and contains a table with data template tags. It then creates a DataSet,
  19. // loads the DsNWind.xml data base into it, and builds a product list
  20. // from tables in that data set. The product list is set as the template
  21. // data source on the document, and the template is processed to build
  22. // the final document with actual data.
  23. public class DataTplProductList
  24. {
  25. public GcWordDocument CreateDocx(string[] sampleParams)
  26. {
  27. var doc = new GcWordDocument();
  28.  
  29. // Load the template DOCX:
  30. doc.Load(Path.Combine("Resources", "WordDocs", sampleParams[3]));
  31.  
  32. using var ds = new DataSet();
  33. // Load the data and build the product list data source:
  34. ds.ReadXml(Path.Combine("Resources", "data", "DsNWind.xml"));
  35.  
  36. DataTable dtProds = ds.Tables["Products"];
  37. DataTable dtSupps = ds.Tables["Suppliers"];
  38.  
  39. var products =
  40. from prod in dtProds.Select()
  41. join supp in dtSupps.Select()
  42. on prod["SupplierID"] equals supp["SupplierID"]
  43. orderby prod["ProductName"]
  44. select new
  45. {
  46. ProductID = prod["ProductID"],
  47. ProductName = prod["ProductName"],
  48. Supplier = supp["CompanyName"],
  49. QuantityPerUnit = prod["QuantityPerUnit"],
  50. UnitPrice = prod["UnitPrice"]
  51. };
  52.  
  53. // Add the data source to the data template data sources:
  54. doc.DataTemplate.DataSources.Add("ds", products);
  55.  
  56. // The document already has all the necessary bindings,
  57. // so we only need to process the data template:
  58. doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"));
  59.  
  60. // Done:
  61. return doc;
  62. }
  63.  
  64. public GcWordDocument CreateDocx(int paramIdx = 0)
  65. {
  66. return CreateDocx(GetSampleParamsList()[paramIdx]);
  67. }
  68.  
  69. public static List<string[]> GetSampleParamsList()
  70. {
  71. // Strings are name, description, info, template docx:
  72. return new List<string[]>()
  73. {
  74. new string[] { "@data-templates/Product List",
  75. "Load a template DOCX and bind it to a product list from an XML data set", null,
  76. "ProductListTemplate.docx" },
  77. new string[] { "@data-templates/Formatter Chain",
  78. "Load a template that uses chained formatters to highlight products that cost $50+", null,
  79. "ProductListTemplate-cond.docx" },
  80. };
  81. }
  82. }
  83. }
  84.