Controls You Can Use on Web Forms ASP.NET Data Controls DataGrid Control
The DataGrid 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 DataGrid 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 DataGrid control can automatically page over the data, if the underlying data source supports paging.
The following sections briefly introduce the main features of the DataGrid control.
NOTE: The DataGrid control must be bound to a data source; otherwise, the control will not render on the page.
The DataGrid control provides the following options for binding to data:
- Data binding using the DataSourceID property, which allows binding the DataGrid control to a data source control. This is the recommended approach as it enables the DataGrid 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 DataGrid 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 DataKeyField property to specify the name of the primary key field that uniquely identifies 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 DataGrid control for updating data, see Allowing Users to Edit Rows in a DataGrid Control. For general information about data binding in server controls, see Data Binding in Web Forms.
The DataGrid control supports a Columns collection property, that can be used to set the collection of objects that represent the columns of a DataGrid control.
The following table lists the different column types you can specify for the DataGrid.
Column field 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 display a column of custom button controls, such as an Add or a 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 static text or bound to a field in a data source. Likewise, the destination of each hyperlink can be bound to a field in a data source. |
TemplateColumn |
Displays each item in the column according to a specified template. This allows you to provide custom controls in the column. |
The DataGrid 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 DataGrid.
Style property |
Description |
AlternatingItemStyle |
The style settings for the alternating data rows in the DataGrid control. When this property is set, the data rows are displayed alternating between the ItemStyle settings and the AlternatingItemStyle settings. |
EditItemStyle |
The style settings for the data rows when the DataGrid control is in edit mode. |
FooterStyle |
The style settings for the footer row of the DataGrid control. |
HeaderStyle |
The style settings for the header row of the DataGrid control. |
ItemStyle |
The style settings for the data rows in the DataGrid control. When the AlternatingItemStyle property is also set, the data rows are displayed alternating between the ItemStyle settings and the AlternatingItemStyle settings. |
PagerStyle |
The style settings for the pager row of the DataGrid control. |
SelectedItemStyle |
The style settings for the selected item in the DataGrid control. |
In addition, the DataGrid supports additional customization using Cascading Style Sheets (CSS). For more information, see Web Forms Server Controls and CSS Styles.
Adding DataGrid Controls to a Web Forms Page