Files
docs-desktop/dotnet-desktop-guide/framework/wpf/advanced/how-to-manipulate-table-columns-through-the-columns-property.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

72 lines
4.7 KiB
Markdown

---
title: "How to: Manipulate a Table's Columns through the Columns Property"
ms.date: "03/30/2017"
ms.custom: devdivchpfy22
description: Learn how to manipulate a Table's Columns through the Columns property.
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "documents [WPF], manipulating table columns"
- "properties [WPF], Columns [WPF], manipulating table columns"
- "tables [WPF], manipulating columns"
- "Columns property [WPF]"
ms.assetid: 3f8884f4-7e1f-456b-be06-fbd3cf469bf3
---
# How to: Manipulate a Table's Columns through the Columns Property
This example demonstrates some of the more common operations that can be performed on a table's columns through the <xref:System.Windows.Documents.Table.Columns%2A> property.
## Create a new table
The following example creates a new table and then uses the <xref:System.Windows.Documents.TableColumnCollection.Add%2A> method to add columns to the table's <xref:System.Windows.Documents.Table.Columns%2A> collection.
[!code-csharp[TableSnippets2#_Table_Columns_Add](~/samples/snippets/csharp/VS_Snippets_Wpf/TableSnippets2/CSharp/Window1.xaml.cs#_table_columns_add)]
[!code-vb[TableSnippets2#_Table_Columns_Add](~/samples/snippets/visualbasic/VS_Snippets_Wpf/TableSnippets2/visualbasic/window1.xaml.vb#_table_columns_add)]
## Insert a new TableColumn
The following example inserts a new <xref:System.Windows.Documents.TableColumn>. The new column is inserted at index position 0, making it the new first column in the table.
> [!NOTE]
> The <xref:System.Windows.Documents.TableColumnCollection> collection uses standard zero-based indexing.
[!code-csharp[TableSnippets2#_Table_Columns_Insert](~/samples/snippets/csharp/VS_Snippets_Wpf/TableSnippets2/CSharp/Window1.xaml.cs#_table_columns_insert)]
[!code-vb[TableSnippets2#_Table_Columns_Insert](~/samples/snippets/visualbasic/VS_Snippets_Wpf/TableSnippets2/visualbasic/window1.xaml.vb#_table_columns_insert)]
## Access properties in the TableColumnCollection
The following example accesses some arbitrary properties on columns in the <xref:System.Windows.Documents.TableColumnCollection> collection, referring to particular columns by index.
[!code-csharp[TableSnippets2#_Table_Columns_Manip](~/samples/snippets/csharp/VS_Snippets_Wpf/TableSnippets2/CSharp/Window1.xaml.cs#_table_columns_manip)]
[!code-vb[TableSnippets2#_Table_Columns_Manip](~/samples/snippets/visualbasic/VS_Snippets_Wpf/TableSnippets2/visualbasic/window1.xaml.vb#_table_columns_manip)]
## Get the number of columns in a table
The following example gets the number of columns currently hosted by the table.
[!code-csharp[TableSnippets2#_Table_Columns_Count](~/samples/snippets/csharp/VS_Snippets_Wpf/TableSnippets2/CSharp/Window1.xaml.cs#_table_columns_count)]
[!code-vb[TableSnippets2#_Table_Columns_Count](~/samples/snippets/visualbasic/VS_Snippets_Wpf/TableSnippets2/visualbasic/window1.xaml.vb#_table_columns_count)]
## Remove a column by reference
The following example removes a particular column by reference.
[!code-csharp[TableSnippets2#_Table_Columns_DelRef](~/samples/snippets/csharp/VS_Snippets_Wpf/TableSnippets2/CSharp/Window1.xaml.cs#_table_columns_delref)]
[!code-vb[TableSnippets2#_Table_Columns_DelRef](~/samples/snippets/visualbasic/VS_Snippets_Wpf/TableSnippets2/visualbasic/window1.xaml.vb#_table_columns_delref)]
## Remove a column by index
The following example removes a particular column by index.
[!code-csharp[TableSnippets2#_Table_Columns_DelIndex](~/samples/snippets/csharp/VS_Snippets_Wpf/TableSnippets2/CSharp/Window1.xaml.cs#_table_columns_delindex)]
[!code-vb[TableSnippets2#_Table_Columns_DelIndex](~/samples/snippets/visualbasic/VS_Snippets_Wpf/TableSnippets2/visualbasic/window1.xaml.vb#_table_columns_delindex)]
## Remove all the columns
The following example removes all columns from the table's columns collection.
[!code-csharp[TableSnippets2#_Table_Columns_Clear](~/samples/snippets/csharp/VS_Snippets_Wpf/TableSnippets2/CSharp/Window1.xaml.cs#_table_columns_clear)]
[!code-vb[TableSnippets2#_Table_Columns_Clear](~/samples/snippets/visualbasic/VS_Snippets_Wpf/TableSnippets2/visualbasic/window1.xaml.vb#_table_columns_clear)]
## See also
- [Table Overview](table-overview.md)
- [Define a Table with XAML](how-to-define-a-table-with-xaml.md)
- [Build a Table Programmatically](how-to-build-a-table-programmatically.md)
- [Manipulate a Table's Row Groups through the RowGroups Property](how-to-manipulate-table-row-groups-through-the-rowgroups-property.md)
- [Manipulate a FlowDocument through the Blocks Property](how-to-manipulate-a-flowdocument-through-the-blocks-property.md)
- [Manipulate a Table's Row Groups through the RowGroups Property](how-to-manipulate-table-row-groups-through-the-rowgroups-property.md)