Files
docs-desktop/dotnet-desktop-guide/framework/wpf/advanced/how-to-build-a-table-programmatically.md
T
Pooja PoojariandAndy De George caa62283b8 US-1889327: Suggestion/warning fixes for WPF. (#1314)
* Suggestion/warning fixes for US-1889327.

* Updated H2s.

* Minor content edit.

* Review edits.

* Minor edits.

* Update dotnet-desktop-guide/framework/wpf/advanced/localization-attributes-and-comments.md

Co-authored-by: Andy (Steve) De George <[email protected]>
2022-02-23 10:27:19 -08:00

4.4 KiB

title, ms.date, ms.custom, description, dev_langs, helpviewer_keywords, ms.assetid
title ms.date ms.custom description dev_langs helpviewer_keywords ms.assetid
How to: Build a Table Programmatically 03/30/2017 devdivchpfy22 Learn how to build a table programmatically.
csharp
vb
tables [WPF], creating programmatically
e3ca88f3-6e94-4b61-82fc-42104c10b761

How to: Build a Table Programmatically

The following examples show how to programmatically create a xref:System.Windows.Documents.Table and populate it with content. The contents of the table are apportioned into five rows (represented by xref:System.Windows.Documents.TableRow objects contained in a xref:System.Windows.Documents.Table.RowGroups%2A object) and six columns (represented by xref:System.Windows.Documents.TableColumn objects). The rows are used for different presentation purposes, including a title row intended to title the entire table, a header row to describe the columns of data in the table, and a footer row with summary information. Note that the notion of "title", "header", and "footer" rows are not inherent to the table; these are simply rows with different characteristics. Table cells contain the actual content, which can be comprised of text, images, or nearly any other [!INCLUDETLA#tla_ui] element.

Create a table

First, a xref:System.Windows.Documents.FlowDocument is created to host the xref:System.Windows.Documents.Table, and a new xref:System.Windows.Documents.Table is created and added to the contents of the xref:System.Windows.Documents.FlowDocument.

[!code-csharpTableSnippets#_TableCreate] [!code-vbTableSnippets#_TableCreate]

Add columns

Next, six xref:System.Windows.Documents.TableColumn objects are created and added to the table's xref:System.Windows.Documents.Table.Columns%2A collection, with some formatting applied.

Note

Note that the table's xref:System.Windows.Documents.Table.Columns%2A collection uses standard zero-based indexing.

[!code-csharpTableSnippets#_TableCreateColumns] [!code-vbTableSnippets#_TableCreateColumns]

Add a title row

Next, a title row is created and added to the table with some formatting applied. The title row happens to contain a single cell that spans all six columns in the table.

[!code-csharpTableSnippets#_TableAddTitleRow] [!code-vbTableSnippets#_TableAddTitleRow]

Add a header row

Next, a header row is created and added to the table, and the cells in the header row are created and populated with content.

[!code-csharpTableSnippets#_TableAddHeaderRow] [!code-vbTableSnippets#_TableAddHeaderRow]

Add a row

Next, a row for data is created and added to the table, and the cells in this row are created and populated with content. Building this row is similar to building the header row, with slightly different formatting applied.

[!code-csharpTableSnippets#_TableAddDataRow] [!code-vbTableSnippets#_TableAddDataRow]

Finally, a footer row is created, added, and formatted. Like the title row, the footer contains a single cell that spans all six columns in the table.

[!code-csharpTableSnippets#_TableAddFooterRow] [!code-vbTableSnippets#_TableAddFooterRow]

See also