如何:透過 RowGroups 屬性管理資料表的資料列群組

此範例示範可透過 RowGroups 屬性在資料表的資料列群組上執行的一些較常見的作業。

使用 Add 方法建立新的資料表

下列範例會建立新的資料表,然後使用 Add 方法將資料行加入至資料表的 RowGroups 集合。

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

插入新的 TableRowGroup

下列範例會插入新的 TableRowGroup 。 新的資料行會插入索引位置 0,使其成為資料表中的新第一個資料列群組。

注意

集合 TableRowGroupCollection 使用標準以零起始的索引。

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

將資料列新增至 TableRowGroup

下列範例會將數個數據列新增至資料表中的特定 TableRowGroup 資料列(由索引指定)。

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

存取第一個資料列群組中的資料列屬性

下列範例會存取資料表中第一個資料列群組中資料列的一些任意屬性。

// 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"

將儲存格新增至 TableRow

下列範例會將數個儲存格新增至資料表中的特定 TableRow 資料格(由索引指定)。

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

存取第一個資料列群組中儲存格的方法和屬性

下列範例會存取第一個資料列群組中第一列儲存格上的一些任意方法和屬性。

// 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()

取得資料表中的 TableRowGroup 元素數目

下列範例會傳回資料表所裝載的專案 TableRowGroup 數目。

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

依參考移除資料列群組

下列範例會依參考移除特定資料列群組。

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

依索引移除資料列群組

下列範例會依索引移除特定資料列群組。

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

從資料表的資料列群組集合中移除所有資料列群組

下列範例會從資料表的資料列群組集合中移除所有資料列群組。

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

另請參閱