asp.net.ph

Skip Navigation Links

Introduction to the GridView Control

Controls You Can Use on Web Forms   ASP.NET Data Controls   GridView Control


The GridView Web server control is a full-featured data-bound control that displays data in tabular format, and provides the ability to sort, select, edit, and delete records from its associated data source.

The GridView control displays one row for each record in the data source, and by default, generates a column for each field. You can, however, choose which fields in the data source will render in the grid.

The GridView control can automatically page over the data, if the underlying data source supports paging.

The following sections briefly introduce the main features of the GridView control.

Binding Data to the Control

NOTE: The GridView control must be bound to a data source; otherwise, the control will not render on the page.

The GridView control provides the following options for binding to data:

  • Data binding using the DataSourceID property, which allows binding the GridView control to a data source control. This is the recommended approach as it enables the GridView control to take advantage of the capabilities of the data source control, and provide built-in functionality for sorting, paging and updating.
  • Data binding using the DataSource property, which allows binding to any data source that implements the ICollection or IEnumerable interface, including ADO.NET datasets and datareaders. This approach, though, requires coding for any additional functionality such as sorting, paging and updating.

When binding to a data source using the DataSourceID property, the GridView control supports two-way data binding. In addition to displaying data, the control automatically supports update and delete operations on the bound data. For more information, see Binding to Data Using a Data Source Control.

When using the control to allow users to edit or delete data, you have to set the DataKeyNames property to specify the name(s) of the primary key field(s) that uniquely identify each record in the data source. This information is needed for the automatic update features to work, but does not have to be part of the information displayed in the form.

For details on using the GridView control for updating data, see Allowing Users to Edit Rows in a GridView Control. For general information about data binding in server controls, see Data Binding in Web Forms.

Customizing the GridView Control User Interface

The GridView control supports a Columns collection property, that is similar in function to the Fields collection of the DetailsView control, except that the GridView control renders each field as a column instead of a row.

The following table lists the different field types you can specify for the GridView.

Column field type Description
BoundField Displays the value of a field in a data source as text.
ButtonField Displays a command button in the GridView control. This allows you to display a row with a custom button control, such as an Add or a Remove button.
CheckBoxField Displays a check box in the GridView control. This field type is commonly used to display fields with a Boolean value.
CommandField Displays built-in command buttons to perform select, edit, or delete operations in the GridView control.
HyperLinkField Displays the value of a field in a data source as a hyperlink. This field type allows you to bind a second field to the hyperlink’s URL.
ImageField Displays an image in the GridView control.
TemplateField Displays user-defined content for a row in the GridView control according to a specified template.

Templates for Control Layout

The GridView control supports additional customization through templates, which offer more control over the rendering of certain elements. The following table lists the template types you can specify for the GridView.

Template type Description
EmptyDataTemplate Defines the content for the empty data row displayed when the GridView control is bound to a data source that does not contain any records. This template usually contains content to alert the user that the data source does not contain any records.
PagerTemplate Defines the content for the pager row displayed when the paging feature is enabled (when the AllowPaging property is set to true). This template usually contains controls with which the user can navigate to another record.

NOTE: The GridView control has a built-in pager row user interface (UI). You need to create a pager template only if you want to create your own custom pager row.

NOTE: You can also create a template for an individual field by adding a TemplateField object to the Columns collection.

Styles

The GridView control also supports additional customization of the user interface by using style properties. The following table lists the style properties you can specify for the GridView.

Style property Description
AlternatingRowStyle The style settings for the alternating data rows in the GridView control. When this property is set, the data rows are displayed alternating between the RowStyle settings and the AlternatingRowStyle settings.
EditRowStyle The style settings for the data rows when the GridView control is in edit mode.
EmptyDataRowStyle The style settings for the empty data row displayed in the GridView control when the data source does not contain any records.
FooterStyle The style settings for the footer row of the GridView control.
HeaderStyle The style settings for the header row of the GridView control.
PagerStyle The style settings for the pager row of the GridView control.
RowStyle The style settings for the data rows in the GridView control. When the AlternatingRowStyle property is also set, the data rows are displayed alternating between the RowStyle settings and the AlternatingRowStyle settings.

For more information, see Web Forms Server Controls and CSS Styles.

See Also

Adding GridView Controls to a Web Forms Page



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note