Export (0) Print
Expand All

DataGrid.Columns Property

Gets a collection of objects that represent the columns of the DataGrid control.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

[<PersistenceModeAttribute(PersistenceMode.InnerProperty)>]
abstract Columns : DataGridColumnCollection with get
[<PersistenceModeAttribute(PersistenceMode.InnerProperty)>]
override Columns : DataGridColumnCollection with get

Property Value

Type: System.Web.UI.WebControls.DataGridColumnCollection
A DataGridColumnCollection object that contains a collection of objects that represent the columns of the DataGrid control.

Use this property to programmatically control the collection of columns in the DataGrid control. The Columns collection contains explicitly declared columns that get rendered in the DataGrid control.

NoteNote

Explicitly declared columns may be used in conjunction with automatically generated columns. When using both, explicitly declared columns will be rendered first, followed by the automatically generated columns. Automatically generated columns are not added to the Columns collection.

The order that the columns are displayed in the DataGrid control is controlled by the order that the columns appear in the Columns collection.

The following table shows the different column classes that derive from the DataGridColumn class and can be used in the Columns collection.

Column type

Description

BoundColumn

Displays a column bound to a field in a data source. It displays each item in the field as text. This is the default column type of the DataGrid control.

ButtonColumn

Displays a command button for each item in the column. This allows you to create a column of custom button controls, such as the Add or Remove button.

EditCommandColumn

Displays a column that contains editing commands for each item in the column.

HyperLinkColumn

Displays the contents of each item in the column as a hyperlink. The contents of the column can be bound to a field in a data source or static text.

TemplateColumn

Displays each item in the column following a specified template. This allows you to provide custom controls in the column.

NoteNote

Although you can programmatically add columns to the Columns collection, it is easier to list the columns statically and then use the Visible property to show or hide the column.

The following code example demonstrates how to use the Columns collection to dynamically add a column to the DataGrid control.

No code example is currently available or this language may not be supported.
No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft