Como manipular grupos de linhas de uma tabela por meio da propriedade RowGroups

Este exemplo demonstra algumas das operações mais comuns que podem ser executadas nos grupos de linhas de uma tabela por meio da RowGroups propriedade.

Criar uma nova tabela usando o método Add

O exemplo a seguir cria uma nova tabela e usa o Add método para adicionar colunas à coleção da RowGroups tabela.

Table tbl = new Table();
int rowGroupsToAdd = 4;
for (int x = 0; x < rowGroupsToAdd; x++)
    tbl.RowGroups.Add(new TableRowGroup());
Dim tbl As New Table()
Dim rowGroupsToAdd As Integer = 4
For x As Integer = 0 To rowGroupsToAdd - 1
    tbl.RowGroups.Add(New TableRowGroup())
Next x

Insere um novo TableRowGroup

O exemplo a seguir insere um novo TableRowGrouparquivo . A nova coluna é inserida na posição de índice 0, tornando-a o novo grupo da primeira linha na tabela.

Observação

A TableRowGroupCollection coleção usa indexação padrão baseada em zero.

tbl.RowGroups.Insert(0, new TableRowGroup());
tbl.RowGroups.Insert(0, New TableRowGroup())

Adicionar linhas ao TableRowGroup

O exemplo a seguir adiciona várias linhas a um determinado TableRowGroup (especificado por índice) na tabela.

int rowsToAdd = 10;
for (int x = 0; x < rowsToAdd; x++)
    tbl.RowGroups[0].Rows.Add(new TableRow());
Dim rowsToAdd As Integer = 10
For x As Integer = 0 To rowsToAdd - 1
    tbl.RowGroups(0).Rows.Add(New TableRow())
Next x

Acessar propriedades de linha no primeiro grupo de linhas

O exemplo a seguir acessa algumas propriedades arbitrárias em linhas no primeiro grupo de linhas na tabela.

// Alias the working TableRowGroup for ease in referencing.
TableRowGroup trg = tbl.RowGroups[0];
trg.Rows[0].Background = Brushes.CornflowerBlue;
trg.Rows[1].FontSize = 24;
trg.Rows[2].ToolTip = "This row's tooltip";
' Alias the working TableRowGroup for ease in referencing.
Dim trg As TableRowGroup = tbl.RowGroups(0)
trg.Rows(0).Background = Brushes.CornflowerBlue
trg.Rows(1).FontSize = 24
trg.Rows(2).ToolTip = "This row's tooltip"

Adicionar células a um TableRow

O exemplo a seguir adiciona várias células a um determinado TableRow (especificado por índice) na tabela.

int cellsToAdd = 10;
for (int x = 0; x < cellsToAdd; x++)
    tbl.RowGroups[0].Rows[0].Cells.Add(new TableCell(new Paragraph(new Run("Cell " + (x + 1)))));
Dim cellsToAdd As Integer = 10
For x As Integer = 0 To cellsToAdd - 1
    tbl.RowGroups(0).Rows(0).Cells.Add(New TableCell(New Paragraph(New Run("Cell " & (x + 1)))))
Next x

Acessar métodos e propriedades em células no primeiro grupo de linhas

O exemplo a seguir acessa alguns métodos e propriedades arbitrários em células na primeira linha no primeiro grupo de linhas.

// Alias the working for for ease in referencing.
TableRow row = tbl.RowGroups[0].Rows[0];
row.Cells[0].Background = Brushes.PapayaWhip;
row.Cells[1].FontStyle = FontStyles.Italic;
// This call clears all of the content from this cell.
row.Cells[2].Blocks.Clear();
' Alias the working for for ease in referencing.
Dim row As TableRow = tbl.RowGroups(0).Rows(0)
row.Cells(0).Background = Brushes.PapayaWhip
row.Cells(1).FontStyle = FontStyles.Italic
' This call clears all of the content from this cell.
row.Cells(2).Blocks.Clear()

Obter o número de elementos TableRowGroup em uma tabela

O exemplo a seguir retorna o número de TableRowGroup elementos hospedados pela tabela.

int rowGroups = tbl.RowGroups.Count;
Dim rowGroups As Integer = tbl.RowGroups.Count

Remover um grupo de linhas por referência

O exemplo a seguir remove um grupo de linhas específico por referência.

tbl.RowGroups.Remove(tbl.RowGroups[0]);
tbl.RowGroups.Remove(tbl.RowGroups(0))

Remover um grupo de linhas por índice

O exemplo a seguir remove um grupo de linhas específico por índice.

tbl.RowGroups.RemoveAt(0);
tbl.RowGroups.RemoveAt(0)

Remover todos os grupos de linhas da coleção de grupos de linhas da tabela

O exemplo a seguir remove todos os grupos de linhas da coleção de grupos de linhas da tabela.

tbl.RowGroups.Clear();
tbl.RowGroups.Clear()

Confira também