AlternatingRows.vb
  1. ''
  2. '' This code is part of Document Solutions for Word demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.Drawing
  6. Imports GrapeCity.Documents.Word
  7.  
  8. '' This sample uses conditional table formatting to build a table
  9. '' with alternating row backgrounds.
  10. Public Class AlternatingRows
  11. Function CreateDocx() As GcWordDocument
  12. Dim doc = New GcWordDocument()
  13.  
  14. '' Random-ish table dimensions:
  15. Dim rand = Util.NewRandom()
  16. Dim rows = rand.Next(10, 100)
  17. Dim cols = rand.Next(4, 6)
  18.  
  19. doc.Body.Paragraphs.Add(
  20. $"A {cols} columns by {rows} rows table, with conditional styles applied to alternating rows:")
  21.  
  22. '' Add an empty table:
  23. Dim t = doc.Body.Tables.Add(0, 0)
  24.  
  25. '' Add some rows and cells to it:
  26. Dim cells = New List(Of String)(cols)
  27. For col = 1 To cols
  28. cells.Add(String.Empty)
  29. Next
  30. For row = 0 To rows - 1
  31. For col = 0 To cols - 1
  32. cells(col) = $"Row {row + 1}, col {col + 1}"
  33. Next
  34. t.Rows.Add(cells.ToArray())
  35. Next
  36.  
  37. '' Create a table style on which we will define conditional formatting:
  38. Dim ts1 = doc.Styles.Add("Table Style 1", StyleType.Table)
  39. '' And assign the style to the table:
  40. t.Style = ts1
  41.  
  42. '' Set up simple borders:
  43. For Each border In ts1.Table.Borders
  44. border.LineStyle = LineStyle.Single
  45. border.LineWidth = 0.5F
  46. border.Color.RGB = Color.DarkGray
  47. Next
  48. '' Add some padding:
  49. ts1.Table.Padding.All = 2
  50.  
  51. '' To use certain table styles, we need to set corresponding flags on the table's style options:
  52. t.Format.StyleOptions = TableStyleOptions.RowBands
  53.  
  54. '' Set up the style to use alternating background colors on odd and even rows:
  55. ts1.Table.RowStripe = 1
  56. '' Odd rows' style:
  57. ts1.Table.Conditionals(TableStyleOverride.Band1Horizontal).Shading.Texture = TexturePattern.Clear
  58. ts1.Table.Conditionals(TableStyleOverride.Band1Horizontal).Shading.BackgroundPatternColor.RGB = Color.FromArgb(&HE6, &HFF, &HE6)
  59. '' Even rows' style:
  60. ts1.Table.Conditionals(TableStyleOverride.Band2Horizontal).Shading.Texture = TexturePattern.Clear
  61. ts1.Table.Conditionals(TableStyleOverride.Band2Horizontal).Shading.BackgroundPatternColor.RGB = Color.FromArgb(&HFF, &HFF, &HE6)
  62.  
  63. '' Done:
  64. Return doc
  65. End Function
  66. End Class
  67.