Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [234]

By Root 5385 0
anything. If an empty data source object is bound and an EmptyDataTemplate template is specified, the results shown to the user have a friendlier look:

There's no data to show in this view.

The EmptyDataTemplate property is ignored if the bound data source is not empty.

When you use a declared set of columns, the AutoGenerateColumns property of the grid is typically set to false. However, this is not a strict requirement—a grid can have declared and autogenerated columns. In this case, declared columns appear first. Note also that autogenerated columns are not added to the Columns collection. As a result, when column autogeneration is used, the Columns collection is typically empty.

Configuring Columns


The Columns property is a collection of DataControlField objects. The DataControlField object is akin to the DataGrid’s DataGridColumn object, but it has a more general name because these field objects can be reused in other data-bound controls that do not necessarily render columns. (For example, in the DetailsView control, the same class is used to render a row.)

You can define your columns either declaratively or programmatically. In the latter case, you just instantiate any needed data field objects and add them to the Columns collection. The following code adds a data-bound column to the grid:

var field = new BoundField();

field.DataField = "companyname";

field.HeaderText = "Company Name";

grid.ColumnFields.Add(field);

Columns of data are displayed in the order that the column fields appear in the collection. To statically declare your columns in the .aspx source file, you use the tag, as shown here:

Table 10-11 lists the column field classes that can be used in a GridView control. All the classes inherit DataControlField.

Table 10-11. Supported Column Types in GridView Controls

Type

Description

BoundField

Default column type, displays the value of a field as plain text.

ButtonField

Displays the value of a field as a command button. You can choose the link or the push button style.

CheckBoxField

Displays the value of a field as a check box. It is commonly used to render Boolean values.

CommandField

Enhanced version of ButtonField, represents a special command such as Select, Delete, Insert, or Update. It’s rarely useful with GridView controls; the field is tailor-made for DetailsView controls. (GridView and DetailsView share the set of classes derived from DataControlField.)

HyperLinkField

Displays the value of a field as a hyperlink. When the hyperlink is clicked, the browser navigates to the specified URL.

ImageField

Displays the value of a field as the Src property of an HTML tag. The content of the bound field should be the URL to the physical image.

TemplateField

Displays user-defined content for each item in the column. You use this column type when you want to create a custom column field. The template can contain any number of data fields combined with literals, images, and other controls.

Table 10-12 lists the main properties shared by all column types.

Table 10-12. Common Properties of GridView Columns

Property

Description

AccessibleHeaderText

The text that represents abbreviated text read by screen readers of Assistive Technology devices.

FooterStyle

Gets the style object for the column’s footer.

FooterText

Gets and sets the text for the column’s footer.

HeaderImageUrl

Gets and sets the URL of the image to place in the column’s header.

HeaderStyle

Gets the style object for the column’s header.

HeaderText

Gets and sets the text for the column’s header.

InsertVisible

Indicates whether the field is visible when its parent data-bound control is in insert mode. This property does not apply to GridView

Return Main Page Previous Page Next Page

®Online Book Reader