System.Web.UI.WebControls Namespace DataGrid Class
Returns a collection of objects that represent the columns of the DataGrid control.
Script |
[ DataGridColumnCollection variable = ] DataGrid.Columns |
This property can only be used programmatically; it cannot be set when declaring the control.
The property is read only with no default value.
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.
NOTE: 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. |
NOTE: While 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.
DataGrid Members DataGridColumn BoundColumn ButtonColumn EditCommandColumn HyperLinkColumn TemplateColumn