System.Web.UI.WebControls Namespace DataControlFieldCollection Class
.NET Framework version 2.0
Represents the collection of Columns in a GridView control.
Use the DataControlFieldCollection to programmatically manage a collection of DataControlField derived column objects. These objects represent the columns in a GridView control. You can add, remove, or insert columns into the DataControlFieldCollection.
NOTE: When the AutoGenerateColumns property is set to true, the columns created by the GridView control are not added to the Columns collection.
The GridView control does not store the contents of its Columns collection into the view state. To add or remove a column dynamically, you must programmatically add or remove the column everytime the page is refreshed. Provide a Page_Init function that adds or removes the column before the GridView control's state is reload and the control is rebuilt. Otherwise, the changes to the Columns collection are not reflected in the GridView control when it is displayed.
NOTE: While you can programmatically add columns to or remove columns from the Columns collection of the GridView control, it is easier to list the columns statically and then use the 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 GridView control.
The following table lists the different column classes that derive from the DataControlField class.
Column Type |
Description |
BoundField |
Displays a column bound to a field in a data source, that displays each row in the field as text. This is the default column type of the GridView control. |
ButtonField |
Displays a command button for each row in the column. This allows you to create a column of custom button controls, such as the Add or Remove button. |
CheckBoxField |
Displays a check box for each row in the GridView control. This column field type is commonly used to display fields with a Boolean value. |
CommandField |
Displays predefined command buttons to perform selecting, editing, or deleting operations. |
HyperLinkField |
Displays the contents of each row in the column as a hyperlink. The contents of the column can be bound to a field in a data source or static text. |
ImageField |
Displays an image for each row in the GridView control. |
TemplateField |
Displays each row in the column following a specified template. This allows you to provide custom controls in the column. |
NOTE: The DataControlField class is the base class for the column classes listed. It is not used directly in the DataControlFieldCollection.
GridView DataControlField BoundField ButtonField CheckBoxField CommandField HyperLinkField ImageField TemplateField