AlternatingRows.vb
''
'' This code is part of GrapeCity Documents for Word samples.
'' Copyright (c) GrapeCity, Inc. All rights reserved.
''
Imports System.Drawing
Imports GrapeCity.Documents.Word

'' This sample uses conditional table formatting to build a table
'' with alternating row backgrounds.
Public Class AlternatingRows
    Function CreateDocx() As GcWordDocument
        Dim doc = New GcWordDocument()

        '' Random-ish table dimensions:
        Dim rand = New Random(Convert.ToInt32(DateTime.Now.Ticks And Int32.MaxValue))
        Dim rows = rand.Next(10, 100)
        Dim cols = rand.Next(4, 6)

        Dim section = doc.Body.Sections.First
        section.GetRange().Paragraphs.Add(
            $"A {cols} columns by {rows} rows table, with conditional styles applied to alternating rows:")

        '' Add an empty table:
        Dim t = section.GetRange().Tables.Add(0, 0)

        '' Add some rows and cells to it:
        Dim cells = New List(Of String)(cols)
        For col = 1 To cols
            cells.Add(String.Empty)
        Next
        For row = 0 To rows - 1
            For col = 0 To cols - 1
                cells(col) = $"Row {row + 1}, col {col + 1}"
            Next
            t.Rows.Add(cells.ToArray())
        Next

        '' Create a table style on which we will define conditional formatting:
        Dim ts1 = doc.Styles.Add("Table Style 1", StyleType.Table)
        '' And assign the style to the table:
        t.Style = ts1

        '' Set up simple borders:
        For Each border In ts1.Table.Borders
            border.LineStyle = LineStyle.Single
            border.LineWidth = 0.5F
            border.Color.RGB = Color.DarkGray
        Next
        '' Add some padding:
        ts1.Table.Padding.All = 2

        '' To use certain table styles, we need to set corresponding flags on the table's style options:
        t.Format.StyleOptions = TableStyleOptions.RowBands

        '' Set up the style to use alternating background colors on odd and even rows:
        ts1.Table.RowStripe = 1
        '' Odd rows' style:
        ts1.Table.Conditionals(TableStyleOverride.Band1Horizontal).Shading.Texture = TexturePattern.Clear
        ts1.Table.Conditionals(TableStyleOverride.Band1Horizontal).Shading.BackgroundPatternColor.RGB = Color.FromArgb(&HE6, &HFF, &HE6)
        '' Even rows' style:
        ts1.Table.Conditionals(TableStyleOverride.Band2Horizontal).Shading.Texture = TexturePattern.Clear
        ts1.Table.Conditionals(TableStyleOverride.Band2Horizontal).Shading.BackgroundPatternColor.RGB = Color.FromArgb(&HFF, &HFF, &HE6)

        '' Done:
        Return doc
    End Function
End Class