When you are working with a DataGrid Web server control, you can build several different types of columns into the grid:
- Bound columns
- Button columns
- Hyperlink columns
- Template columns
In addition, it is also possible to allow the grid to auto-generate columns based on information in the grid's data source.
If the DataGrid control's AutoGenerateColumn property is set to true, the control will generate and display a bound column for each bindable column in the data source. Bindable column types include primitive types, string, datetime, and decimal. If you have a field in your data source that is not a supported column type, then the control does not create a column for that field. As long as your data source has at least one bindable column, the DataGrid control binds to the source and generates the bindable columns, and ignores those that are not. If AutoGenerateColumns is true and you create explicit columns, the grid will show both your explicit columns and the auto-generated ones.
Using auto-generated columns is a quick and easy way to display information in the grid. Because these columns are automatically generated at run time and displayed in the order in which they occur in the data source, you do not have flexibility in how these columns are laid out. Therefore, using auto-generated columns is recommended primarily for simple data display, prototype pages, and other situations where you do not necessarily need to lay the columns out carefully. You should explicitly define columns when you want to control which columns to display and in what order.
A bound column (BoundColumn element) displays information from one column of a data source. You typically add one bound column to the grid for every data column you want to display. Use bound columns when you want to choose which data columns to display in the grid. Bound columns also allow you to set a variety of properties to specify features such as:
- The text, font, and color of the column header and footer.
- Column width.
- Data format.
- Whether the column is read-only (that is, whether it will display an editable control when the row is in edit mode).
For details on creating bound columns in the Visual Studio Web Forms Designer, see Adding Bound Columns to a DataGrid Web Server Control. For details on creating bound columns, see DataGrid Web Server Control.
Button columns allow you to add grid columns with buttons that allow users to perform row-specific functions such as selecting the current row, deleting it, putting it into edit mode. You can create buttons for predefined functions or create your own functionality (for example, an "Add to Shopping Cart" button).
You can define two types of button columns:
- An EditCommandColumn element is predefined to contain three buttons. In display mode, the column displays an Edit button that users can click to put the row into edit mode (with editable controls). When the row is in edit mode, the Edit button is replaced with two buttons: an Update button and a Cancel button. You can specify the text (caption) of the buttons.
- A ButtonColumn element can contain any arbitrary button. You can use a ButtonColumn element to define certain predefined functionality (such as selecting a row or deleting it) or to create buttons for your own functionality.
When you define a button for a button column, you specify the button's text (caption) and its button type (LinkButton or Button). In addition, for buttons in the ButtonColumn element, you specify a special CommandName property. The setting for this property determines what happens when the button is clicked. For example:
|Select||Clicking the button automatically selects the row.|
|Delete||Clicking the button raises the DataGrid control's DeleteCommand.|
|A custom (non-predefined) value||Clicking the button raises the DataGrid control's generic ItemCommand.|
Note Settings for the CommandName property are case sensitive.
In most cases, the button click raises an event that you then handle. (If the CommandName property is set to "Select," the event is raised, but it is also handled automatically by the grid to select the specified rows.) Typically, in your handler you determine which row was affected and the appropriate action — for example, setting that row into edit mode (or canceling it), deleting the data row represented by that grid row, and so on. For custom buttons, the grid raises the ItemCommand event and passes the value of the CommandName property as part of the EventArgs object.
For details on creating button columns in the Visual Studio Web Forms Designer, see Adding Button Columns to a DataGrid Web Server Control. For details on creating button columns, see DataGrid Web Server Control.
For implementing the functionality of button columns, see the following topics:
- Allowing Users to Edit Items in a DataGrid Web Server Control
- Allowing Users to Select Items in a DataGrid Web Server Control
- Allowing Users to Delete Items in a DataGrid Web Server Control
A hyperlink column (HyperLinkColumn element) displays a link in each row. You can specify static text for the hyperlink or derive the link's text from a data column. Similarly, you can specify a static URL as the link's target or get the URL from a data source.
For details on creating hyperlink columns in the Visual Studio Web Forms Designer, see Adding Hyperlink Columns to a DataGrid Web Server Control. For details on creating button columns as HTML elements, see DataGrid Web Server Control.
A template column allows you to create a custom layout for a column based on any mixture of controls and static text. Template columns are similar to templates used for the DataList and Repeater controls, with the difference that you are creating the layout for just a column rather than for a row-like item. For details about templates, see Web Server Controls Templates.
Because they give you complete control over the contents of the column, template columns provide the greatest flexibility for column layout. Some examples include:
- You can easily mix static text with controls.
- You can specify what controls are used in edit mode. When a bound column is in edit mode, it displays a text box no matter what type of data is being edited. By using a template control, you can specify a check box, a drop-down list, or other appropriate control instead.
Within the template column, you can create up to four separate template types:
- The HeaderTemplate and FooterTemplate define what will appear in the column header and footer, respectively.
- The ItemTemplate defines how information appears in the column when the row is in display mode. You often add controls such as a Label or Literal control to display data.
- The EditItemTemplate defines the controls (and text, if applicable) that appear in the column when it is in edit mode. Typically, this involves text boxes, check boxes, and drop-down lists.
For details on creating template columns in the Visual Studio Web Forms Designer, see Adding Template Columns to a DataGrid Web Server Control. For details on creating template columns as HTML elements, see DataGrid Web Server Control.