Displays the values of a data source in a table where each column represents a field and each row represents a record. The System.Web.UI.WebControls.GridView control enables you to select, sort, and edit these items.
See Also: GridView Members
In this topic:
The System.Web.UI.WebControls.GridView control is used to display the values of a data source in a table. Each column represents a field, while each row represents a record. The System.Web.UI.WebControls.GridView control supports the following features:
Binding to data source controls, such as System.Web.UI.WebControls.SqlDataSource.
Built-in sort capabilities.
Built-in update and delete capabilities.
Built-in paging capabilities.
Built-in row selection capabilities.
Programmatic access to the System.Web.UI.WebControls.GridView object model to dynamically set properties, handle events, and so on.
Multiple key fields.
Multiple data fields for the hyperlink columns.
Customizable appearance through themes and styles.
To learn about the other data-bound controls that are available in ASP.NET, see ASP.NET Data-Bound Web Server Controls Overview.
If you are familiar with the System.Web.UI.WebControls.DataGrid control from the .NET Framework version 1.0, the System.Web.UI.WebControls.GridView control is the successor to the System.Web.UI.WebControls.DataGrid control.
Each column in the System.Web.UI.WebControls.GridView control is represented by a System.Web.UI.WebControls.DataControlField object. By default, the GridView.AutoGenerateColumns property is set to true, which creates an System.Web.UI.WebControls.AutoGeneratedField object for each field in the data source. Each field is then rendered as a column in the System.Web.UI.WebControls.GridView control in the order that each field appears in the data source.
You can also manually control which column fields appear in the System.Web.UI.WebControls.GridView control by setting the GridView.AutoGenerateColumns property to false and then defining your own column field collection. Different column field types determine the behavior of the columns in the control. The following table lists the different column field types that can be used.
System.Web.UI.WebControls.BoundField |
Displays the value of a field in a data source. This is the default column type of the System.Web.UI.WebControls.GridView control. |
System.Web.UI.WebControls.ButtonField |
Displays a command button for each item in the System.Web.UI.WebControls.GridView control. This enables you to create a column of custom button controls, such as the Add or the Remove button. |
System.Web.UI.WebControls.CheckBoxField |
Displays a check box for each item in the System.Web.UI.WebControls.GridView control. This column field type is commonly used to display fields with a Boolean value. |
System.Web.UI.WebControls.CommandField |
Displays predefined command buttons to perform select, edit, or delete operations. |
System.Web.UI.WebControls.HyperLinkField |
Displays the value of a field in a data source as a hyperlink. This column field type enables you to bind a second field to the hyperlink's URL. |
System.Web.UI.WebControls.ImageField |
Displays an image for each item in the System.Web.UI.WebControls.GridView control. |
System.Web.UI.WebControls.TemplateField |
Displays user-defined content for each item in the System.Web.UI.WebControls.GridView control according to a specified template. This column field type enables you to create a custom column field. |
To define a column field collection declaratively, first add opening and closing <Columns> tags between the opening and closing tags of the System.Web.UI.WebControls.GridView control. Next, list the column fields that you want to include between the opening and closing <Columns> tags. The columns specified are added to the GridView.Columns collection in the order listed. The GridView.Columns collection stores all the column fields in the control and enables you to programmatically manage the column fields in the System.Web.UI.WebControls.GridView control.
Explicitly declared column fields can be displayed in combination with automatically generated column fields. When both are used, explicitly declared column fields are rendered first, followed by the automatically generated column fields.
Automatically generated column fields are not added to the GridView.Columns collection.
The System.Web.UI.WebControls.GridView control can be bound to a data source control (such as the System.Web.UI.WebControls.SqlDataSource control or System.Web.UI.WebControls.ObjectDataSource control) or to any data source collection that implements the IEnumerable interface, such as System.Data.DataView, ArrayList, List`1, or other collection types. Use one of the following methods to bind the System.Web.UI.WebControls.GridView control to the appropriate data source type:
To bind to a data source control, set the DataBoundControl.DataSourceID property of the System.Web.UI.WebControls.GridView control to the System.Web.UI.Control.ID value of the data source control. The System.Web.UI.WebControls.GridView control automatically binds to the specified data source control and can take advantage of the data source control's capabilities to perform sorting, updating, deleting, and paging. This is the preferred method to bind to data.
To bind to a data source that implements the IEnumerable interface, programmatically set the BaseDataBoundControl.DataSource property of the System.Web.UI.WebControls.GridView control to the data source and then call the BaseDataBoundControl.DataBind method. When using this method, the System.Web.UI.WebControls.GridView control does not provide built-in sort, update, delete, and paging functionality. You need to provide this functionality by using the appropriate event.
For more information about data binding, see Accessing Data with ASP.NET.
This control can be used to display user input, which might include malicious client script. Check any information that is sent from a client for executable script, SQL statements, or other code before displaying it in your application. Whenever possible, it is strongly recommended that values are HTML-encoded before they are displayed in this control (the System.Web.UI.WebControls.BoundField class HTML-encodes values by default). ASP.NET provides an input request validation feature to block script and HTML in user input. Validation server controls are also provided to assess user input. For more information, see Introduction to the Validation Controls.
The System.Web.UI.WebControls.GridView control provides many built-in capabilities that allow the user to sort, update, delete, select, and page through items in the control. When the System.Web.UI.WebControls.GridView control is bound to a data source control, the System.Web.UI.WebControls.GridView control can take advantage of the data source control's capabilities and provide automatic sort, update, and delete functionality.
The System.Web.UI.WebControls.GridView control can provide support for sorting, updating, and deleting with other types of data sources. However, you will need to provide an appropriate event handler with the implementation for these operations.
Sorting allows the user to sort the items in the System.Web.UI.WebControls.GridView control with respect to a specific column by clicking on the column's header. To enable sorting, set the GridView.AllowSorting property to true.
The automatic updating, deleting, and selection functionalities are enabled when a button in a System.Web.UI.WebControls.ButtonField or System.Web.UI.WebControls.TemplateField column field, with a command name of "Edit", "Delete", and "Select", respectively, is clicked. The System.Web.UI.WebControls.GridView control can automatically add a System.Web.UI.WebControls.CommandField column field with an Edit, Delete, or Select button if the GridView.AutoGenerateEditButton, GridView.AutoGenerateDeleteButton, or GridView.AutoGenerateSelectButton property is set to true, respectively.
Inserting records into the data source is not directly supported by the System.Web.UI.WebControls.GridView control. However, it is possible to insert records by using the System.Web.UI.WebControls.GridView control in conjunction with the DetailsView or FormView control. For more information, see System.Web.UI.WebControls.DetailsView or System.Web.UI.WebControls.FormView, respectively.
Instead of displaying all the records in the data source at the same time, the System.Web.UI.WebControls.GridView control can automatically break the records up into pages. To enable paging, set the GridView.AllowPaging property to true.
The System.Web.UI.WebControls.GridView control is re-created on postback based on the information that is stored in System.Web.UI.PageStatePersister.ViewState. If the System.Web.UI.WebControls.GridView control includes a System.Web.UI.WebControls.TemplateField or a System.Web.UI.WebControls.CommandField with the CommandField.CausesValidation property set to true, then the System.Web.UI.Page.EnableViewState property must also be set to true to ensure that concurrent data operations, such as updates and deletes, apply to the appropriate row.
You can customize the appearance of the System.Web.UI.WebControls.GridView control by setting the style properties for the different parts of the control. The following table lists the different style properties.
GridView.AlternatingRowStyle |
The style settings for the alternating data rows in the System.Web.UI.WebControls.GridView control. When this property is set, the data rows are displayed alternating between the GridView.RowStyle settings and the GridView.AlternatingRowStyle settings. |
GridView.EditRowStyle |
The style settings for the row being edited in the System.Web.UI.WebControls.GridView control. |
GridView.EmptyDataRowStyle |
The style settings for the empty data row displayed in the System.Web.UI.WebControls.GridView control when the data source does not contain any records. |
GridView.FooterStyle |
The style settings for the footer row of the System.Web.UI.WebControls.GridView control. |
GridView.HeaderStyle |
The style settings for the header row of the System.Web.UI.WebControls.GridView control. |
GridView.PagerStyle |
The style settings for the pager row of the System.Web.UI.WebControls.GridView control. |
GridView.RowStyle |
The style settings for the data rows in the System.Web.UI.WebControls.GridView control. When the GridView.AlternatingRowStyle property is also set, the data rows are displayed alternating between the GridView.RowStyle settings and the GridView.AlternatingRowStyle settings. |
GridView.SelectedRowStyle |
The style settings for the selected row in the System.Web.UI.WebControls.GridView control. |
GridView.SortedAscendingCellStyle |
The style setting for the data column the data is sorted by in the System.Web.UI.WebControls.GridView control. When this style is set, the style (for example, highlighted column) is applied to cells when the data is sorted in ascending order. |
GridView.SortedAscendingHeaderStyle |
The style setting for the data column the data is sorted by in the System.Web.UI.WebControls.GridView control. When this style is set, an arrow indicating the data is sorted ascending is placed on the header of the System.Web.UI.WebControls.GridView control when the data is sorted in ascending order. |
GridView.SortedDescendingCellStyle |
The style setting for the data column the data is sorted by in the System.Web.UI.WebControls.GridView control. When this style is set, the style (for example, highlighted column) is applied to cells when the data is sorted in descending order. |
GridView.SortedDescendingHeaderStyle |
The style setting for the data column the data is sorted by in the System.Web.UI.WebControls.GridView control. When this style is set, an arrow pointing down is placed on the header of the System.Web.UI.WebControls.GridView when the data is sorted in descending order. |
You can also show or hide different parts of the control. The following table lists the properties that control which parts are shown or hidden.
GridView.ShowFooter |
Shows or hides the footer section of the System.Web.UI.WebControls.GridView control. |
GridView.ShowHeader |
Shows or hides the header section of the System.Web.UI.WebControls.GridView control. |
The System.Web.UI.WebControls.GridView control provides several events that you can program against. This enables you to run a custom routine whenever an event occurs. The following table lists the events that are supported by the System.Web.UI.WebControls.GridView control.
GridView.PageIndexChanged |
Occurs when one of the pager buttons is clicked, but after the System.Web.UI.WebControls.GridView control handles the paging operation. This event is commonly used when you need to perform a task after the user navigates to a different page in the control. |
GridView.PageIndexChanging |
Occurs when one of the pager buttons is clicked, but before the System.Web.UI.WebControls.GridView control handles the paging operation. This event is often used to cancel the paging operation. |
GridView.RowCancelingEdit |
Occurs when a row's Cancel button is clicked, but before the System.Web.UI.WebControls.GridView control exits edit mode. This event is often used to stop the canceling operation. |
GridView.RowCommand |
Occurs when a button is clicked in the System.Web.UI.WebControls.GridView control. This event is often used to perform a task when a button is clicked in the control. |
GridView.RowCreated |
Occurs when a new row is created in the System.Web.UI.WebControls.GridView control. This event is often used to modify the contents of a row when the row is created. |
GridView.RowDataBound |
Occurs when a data row is bound to data in the System.Web.UI.WebControls.GridView control. This event is often used to modify the contents of a row when the row is bound to data. |
GridView.RowDeleted |
Occurs when a row's Delete button is clicked, but after the System.Web.UI.WebControls.GridView control deletes the record from the data source. This event is often used to check the results of the delete operation. |
GridView.RowDeleting |
Occurs when a row's Delete button is clicked, but before the System.Web.UI.WebControls.GridView control deletes the record from the data source. This event is often used to cancel the deleting operation. |
GridView.RowEditing |
Occurs when a row's Edit button is clicked, but before the System.Web.UI.WebControls.GridView control enters edit mode. This event is often used to cancel the editing operation. |
GridView.RowUpdated |
Occurs when a row's Update button is clicked, but after the System.Web.UI.WebControls.GridView control updates the row. This event is often used to check the results of the update operation. |
GridView.RowUpdating |
Occurs when a row's Update button is clicked, but before the System.Web.UI.WebControls.GridView control updates the row. This event is often used to cancel the updating operation. |
GridView.SelectedIndexChanged |
Occurs when a row's Select button is clicked, but after the System.Web.UI.WebControls.GridView control handles the select operation. This event is often used to perform a task after a row is selected in the control. |
GridView.SelectedIndexChanging |
Occurs when a row's Select button is clicked, but before the System.Web.UI.WebControls.GridView control handles the select operation. This event is often used to cancel the selection operation. |
GridView.Sorted |
Occurs when the hyperlink to sort a column is clicked, but after the System.Web.UI.WebControls.GridView control handles the sort operation. This event is commonly used to perform a task after the user clicks a hyperlink to sort a column. |
GridView.Sorting |
Occurs when the hyperlink to sort a column is clicked, but before the System.Web.UI.WebControls.GridView control handles the sort operation. This event is often used to cancel the sorting operation or to perform a custom sorting routine. |
For information about how to configure this control so that it generates markup that conforms to accessibility standards, see Accessibility in Visual Studio 2010 and ASP.NET 4 and ASP.NET Controls and Accessibility.
Example
<asp:GridView AccessKey="string" AllowPaging="True|