System.Web.UI.WebControls.DataGridColumnCollection Class

A collection of System.Web.UI.WebControls.DataGridColumn-derived column objects that represent the columns in a System.Web.UI.WebControls.DataGrid control. This class cannot be inherited.

See Also: DataGridColumnCollection Members


public sealed class DataGridColumnCollection : ICollection, System.Web.UI.IStateManager


Use the System.Web.UI.WebControls.DataGridColumnCollection collection to programmatically manage a collection of System.Web.UI.WebControls.DataGridColumn-derived column objects. These objects represent the columns in a System.Web.UI.WebControls.DataGrid control. You can add, remove, or insert columns in the System.Web.UI.WebControls.DataGridColumnCollection collection.


When the DataGrid.AutoGenerateColumns property is set to true, the columns created by the System.Web.UI.WebControls.DataGrid control are not added to the DataGrid.Columns collection.

The System.Web.UI.WebControls.DataGrid control does not store the contents of its DataGrid.Columns collection in the view state. To add or remove a column dynamically, you must programmatically add or remove the column every time the page is refreshed. Provide a Page_Init function that adds or removes the column before the System.Web.UI.WebControls.DataGrid control's state is reloaded and the control is rebuilt. Otherwise, the changes to the DataGrid.Columns collection are not reflected in the System.Web.UI.WebControls.DataGrid control when it is displayed.


Although you can programmatically add columns to or remove columns from the DataGrid.Columns collection of the System.Web.UI.WebControls.DataGrid control, it is easier to list the columns statically and then use the DataGridColumn.Visible property to display or hide each column.

The order of the columns in the collection determines the order that the columns are displayed in the System.Web.UI.WebControls.DataGrid control.

The following table lists the different column classes that derive from the System.Web.UI.WebControls.DataGridColumn class.


A column that is bound to a field in a data source. It displays each item in the field as text. This is the default column type for the System.Web.UI.WebControls.DataGrid control.


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


A column that contains editing commands for each item in the column.


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


A column that displays each item in the column according to a specified template. This allows you to control the content of the column, for example to display images.


The System.Web.UI.WebControls.DataGridColumn class is the base class for the column classes listed. It is not used directly in the System.Web.UI.WebControls.DataGridColumnCollection collection.


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