Skip to content

Grid

Grid arranges content in rows and cells, following the same shape as an HTML table. Rows are explicit objects: you create the grid with a column count, ask it for a row, and ask the row for cells.

grid := c.NewGrid(2)

row := grid.NewRow()

cell, err := row.NewCell()
if err != nil {
    return err
}
if err := cell.SetContent(p); err != nil {
    return err
}

if err := c.Draw(grid); err != nil {
    return err
}

Grid was introduced in v4 as a more predictable alternative to Table, and for new code it is usually the better starting point. Because a row is a real object rather than an implied group, it is clear which row a cell belongs to, and spans are easier to reason about.

How it differs from Table

Worth knowing before you port existing table code, because two of these differences are silent.

NewMultiCell(colspan, rowspan) takes colspan first. Table.MultiCell(rowspan, colspan) takes rowspan first. A span copied between the two comes out transposed with no error.

Adding a cell that would pass the last column returns an error. A table pushes the overflow onto the next row instead, which tends to surface later as a layout one row taller than intended.

Header rows are not repeated across pages. SetSection(creator.GridRowSectionHeader) marks a row’s semantic role, used to tag cells for accessibility, but it has no effect on layout. If you need column labels repeated on every page of a long listing, Table.SetHeaderRows is still the only thing that does it.

Page wrapping needs no configuration. There is no EnableRowWrap equivalent; a grid longer than the page always continues onto the next one.

Column widths are fractions of the grid width in the range 0 to 1, and you supply one per column. Passing the wrong number returns an error.

Where to look

GuideCovers
Simple gridRows, cells, borders, backgrounds and column widths.
Grids with column spansCells covering several columns.
Grids with row spansCells covering several rows, and how later rows fill around them.
Page wrapping gridsWhat happens when a grid runs past the end of a page.
Last updated on