The GridView control is a data bound control that displays the values of a data source in the form of a table. In this table, each column represents a field and each row represents a record. The GridView control exists within the System.Web.UI .Controls namespace.
When you drag and drop the GridView control on the designer page, the following syntax is added to the source view of the page:
<asp:GridView ID="GridView1" runat="server"> </asp:GridView>
The GridView data control has a built-in capability of sorting, paging, updating, and deleting data. You can also set the column fields through the AutoGenerate property to indicate whether bound fields are automatically created for each field in the data source.
When you drag and drop the GridView control on the designer page, the following syntax is added to the source view of the page:
<asp:GridView ID="GridView1" runat="server"> </asp:GridView>
The GridView data control has a built-in capability of sorting, paging, updating, and deleting data. You can also set the column fields through the AutoGenerate property to indicate whether bound fields are automatically created for each field in the data source.
Column fields types of the GridView class
BoundField: Shows the value of a field in a data source. It is the default column type of the GridView control.
ButtonField: Shows a command button for each item in the GridView control. It allows to create a column of custom button controls, such as the Add or the Remove button.
CheckBoxField: Shows a CheckBox control for each item in the GridView control. This column field type is commonly used to display fields with a Boolean value.
CommandField: Shows predefined command buttons to perform operations, such as select, edit, or delete.
HyperLinkField: Shows the value of a field in a data source as a hyperlink. This column field type allows you to bind a second field to the hyperlink's URL.
ImageField: Shows an image for each item in the GridView control.
TemplateField: Shows user-defined content for each item in the GridView control according to a specified template. This column field type enables us to create a custom column field.
You can also customize the appearance of the GridView control by setting the style properties
EditRowStyle: Performs the style settings for the row that is edited in the GridView control.
EmptyDataRowStyle: Performs the style settings for the empty data row displayed in the GridView control when the data source does not contain any records.
FooterStyle: Performs the style settings for the footer row of the GridView control.
HeaderStyle: Performs the style settings for the header tow of the GridVxew control.
PagerStyle: Performs the style settings for the pager row of the GridView control.
SelectedRowStyle: Performs the style settings for the selected row in the GridView control.
Properties of the GridView Class
AllowPaging: Obtains or sets a value indicating whether the paging feature is enabled.
AllowSorting: Obtains or sets a value indicating whether the sorting feature is enabled.
AlternatingRowStyle: Obtains a reference to the TableltemStyle object that enables us to set the appearance of alternating data rows in a GridView control.
AutoGenerateColumns: Obtains or sets a value indicating whether bound fields are automatically created for each field in the data source.
AutoGenerateDeleteButton: Obtains or sets a value indicating whether a CommandField field column with a Delete button for each data row is automatically added to a GridView control.
AutoGenerateEditButton: Obtains or sets a value indicating whether a CommandField field column with an Edit button for each data row is automatically added to a GridView control.
AutoGenerateSelectButton: Obtains or sets a value indicating whether a CommandField field column with a Select button for each data row is automatically added to a GridView control.
BacklmageUrl: Obtains or sets the URL to an image to display in the background of a GridView control.
BottomPagerRow: Obtains a GridViewRow object that represents the bottom pager row in a GridView control.
Caption; Obtains or sets the text to render in an HTML caption element in a GridView control. This property is provided to make the control more accessible to users of assistive technology devices.
CaptionAlign: Obtains or setsthe horizontal or vertical position of the HTML caption element in a GridView control. This property is provided to make the control more accessible to users of assistive technology devices.
CellPadding: Obtains or sets the amount of space between the contents of a cell and the cell's border.
Cellspacing: Obtains or sets the amount of space between cells.
Columns: Obtains a collection of DataControlField objects that represent the column fields
in a GridView control.
DataKeyNames: Obtains or sets an array that contains the names of the primary key fields for the items displayed in a GridView control.
DataKeys: Obtains a collection of DataKey objects that represent the data key value of each row in a GridView control.
Editlndex: Obtains or sets the index of the row to edit.
EditRowStyle: Obtains a reference to the TableltemStyle object that enables us to set the appearance of the row selected for editing in a GridView control.
EmptyDataRowStyle: Obtains a reference to the TableltemStyle object that enables us to set the appearance of the empty data row rendered when a GridView control is bound to a data source that does not contain any records.
EmptyDataTemplate: Obtains or sets the user-defined content for the empty data row rendered when a GridView control is bound to a data source that does not contain any records.
EmptyDataText: Obtains or sets the text to display in the empty data row rendered when a GridView control is bound to a data source that does not contain any records.
EnableSortingAndPagingCallbacks: Obtains or sets a value indicating whether client-side callbacks are used for sorting and paging operations.
FooterRow: Obtains a GridViewRow object that represents the footer row in a GridView control.
FooterStyle: Obtains a reference to the TableltemStyle object that enables us to set the appearance of the footer row inaGridView control.
GridLines: Obtains or sets the gridline style for a GridView control.
HeaderRow: Obtains a GridViewRow object that represents the header row in a GridView control.
HeaderStyle: Obtains a reference to the TableltemStyle object that enables us to set the appearance of the header row in a GridView control.
HorizontalAlign: Obtains or sets the horizontal alignment of a GridView control on the page.
PageCount: Obtains the number of pages required to display the records of the data source in a GridView control.
Pagelndex: Obtains or sets the index of the currently displayed page.
PagerSettings: Obtains a reference to the PagerSettings object that enables us to set the properties of the pager buttons in a GridView control.
PagerStyle: Obtains a reference to the TableltemStyle object that enables us to set the appearance of the pager row in a GridView control.
PagerTemplate: Obtains or sets the custom content for the pager row in a GridView control.
PageSize: Obtains or sets the number of records to display on a page in a GridView control.
RowHeaderColumn: Obtains or sets the name of the column to use as the column header for the GridView control. This property is provided to make the control more accessible to users of assistive technology devices.
Rows: Obtains a collection of GridViewRow objects that represent the data rows in a GridView control.
RowStyle: Obtains a reference to the TableltemStyle object that enables us to set the appearance of the data rows in a GridView control.
SelectedDataKey: Obtains the DataKey object that contains the data key value for the selected row in a GridView control.
Selectedlndex: Obtains or sets the index of the selected row in a GridView control.
SelectedRow: Obtains a reference to a GridViewRow object that represents the selected row in the control.
SelectedRowStyle: Obtains a reference to the TableltemStyle object that enables us to set the appearance of the selected row in a GridView control.
SelectedValue: Obtains the data key value of the selected row in a GridView control.
ShowFooter: Obtains or sets a value showing whether the footer row is displayed in a GridView control.
ShowHeader: Obtains or sets a value showing whether the header row is displayed in a GridView control.
SortDirection Obtains the sort direction of the column being sorted.
SortExpression Obtains the sort expression associated with the column or columns being sorted.
TopPagerRow Obtains a GridViewRow object that represents the top pager row in a GridView control.
UseAccessibleHeader Obtains or sets a value indicating whether a GridView control renders its header in an accessible format. This property makes the control more accessible to users of assistive technology devices.
AlternatingRowStyle: Obtains a reference to the TableltemStyle object that enables us to set the appearance of alternating data rows in a GridView control.
AutoGenerateColumns: Obtains or sets a value indicating whether bound fields are automatically created for each field in the data source.
AutoGenerateDeleteButton: Obtains or sets a value indicating whether a CommandField field column with a Delete button for each data row is automatically added to a GridView control.
AutoGenerateEditButton: Obtains or sets a value indicating whether a CommandField field column with an Edit button for each data row is automatically added to a GridView control.
AutoGenerateSelectButton: Obtains or sets a value indicating whether a CommandField field column with a Select button for each data row is automatically added to a GridView control.
BacklmageUrl: Obtains or sets the URL to an image to display in the background of a GridView control.
BottomPagerRow: Obtains a GridViewRow object that represents the bottom pager row in a GridView control.
Caption; Obtains or sets the text to render in an HTML caption element in a GridView control. This property is provided to make the control more accessible to users of assistive technology devices.
CaptionAlign: Obtains or setsthe horizontal or vertical position of the HTML caption element in a GridView control. This property is provided to make the control more accessible to users of assistive technology devices.
CellPadding: Obtains or sets the amount of space between the contents of a cell and the cell's border.
Cellspacing: Obtains or sets the amount of space between cells.
Columns: Obtains a collection of DataControlField objects that represent the column fields
in a GridView control.
DataKeyNames: Obtains or sets an array that contains the names of the primary key fields for the items displayed in a GridView control.
DataKeys: Obtains a collection of DataKey objects that represent the data key value of each row in a GridView control.
Editlndex: Obtains or sets the index of the row to edit.
EditRowStyle: Obtains a reference to the TableltemStyle object that enables us to set the appearance of the row selected for editing in a GridView control.
EmptyDataRowStyle: Obtains a reference to the TableltemStyle object that enables us to set the appearance of the empty data row rendered when a GridView control is bound to a data source that does not contain any records.
EmptyDataTemplate: Obtains or sets the user-defined content for the empty data row rendered when a GridView control is bound to a data source that does not contain any records.
EmptyDataText: Obtains or sets the text to display in the empty data row rendered when a GridView control is bound to a data source that does not contain any records.
EnableSortingAndPagingCallbacks: Obtains or sets a value indicating whether client-side callbacks are used for sorting and paging operations.
FooterRow: Obtains a GridViewRow object that represents the footer row in a GridView control.
FooterStyle: Obtains a reference to the TableltemStyle object that enables us to set the appearance of the footer row inaGridView control.
GridLines: Obtains or sets the gridline style for a GridView control.
HeaderRow: Obtains a GridViewRow object that represents the header row in a GridView control.
HeaderStyle: Obtains a reference to the TableltemStyle object that enables us to set the appearance of the header row in a GridView control.
HorizontalAlign: Obtains or sets the horizontal alignment of a GridView control on the page.
PageCount: Obtains the number of pages required to display the records of the data source in a GridView control.
Pagelndex: Obtains or sets the index of the currently displayed page.
PagerSettings: Obtains a reference to the PagerSettings object that enables us to set the properties of the pager buttons in a GridView control.
PagerStyle: Obtains a reference to the TableltemStyle object that enables us to set the appearance of the pager row in a GridView control.
PagerTemplate: Obtains or sets the custom content for the pager row in a GridView control.
PageSize: Obtains or sets the number of records to display on a page in a GridView control.
RowHeaderColumn: Obtains or sets the name of the column to use as the column header for the GridView control. This property is provided to make the control more accessible to users of assistive technology devices.
Rows: Obtains a collection of GridViewRow objects that represent the data rows in a GridView control.
RowStyle: Obtains a reference to the TableltemStyle object that enables us to set the appearance of the data rows in a GridView control.
SelectedDataKey: Obtains the DataKey object that contains the data key value for the selected row in a GridView control.
Selectedlndex: Obtains or sets the index of the selected row in a GridView control.
SelectedRow: Obtains a reference to a GridViewRow object that represents the selected row in the control.
SelectedRowStyle: Obtains a reference to the TableltemStyle object that enables us to set the appearance of the selected row in a GridView control.
SelectedValue: Obtains the data key value of the selected row in a GridView control.
ShowFooter: Obtains or sets a value showing whether the footer row is displayed in a GridView control.
ShowHeader: Obtains or sets a value showing whether the header row is displayed in a GridView control.
SortDirection Obtains the sort direction of the column being sorted.
SortExpression Obtains the sort expression associated with the column or columns being sorted.
TopPagerRow Obtains a GridViewRow object that represents the top pager row in a GridView control.
UseAccessibleHeader Obtains or sets a value indicating whether a GridView control renders its header in an accessible format. This property makes the control more accessible to users of assistive technology devices.
Methods of the GridView Class
DataBind: Binds the data source to the GridView control
DeleteRow: Deletes the record at the specified index from the data source
IsBindableType: Determines whether the specified data type can be bound to a column in a GridView control
Sort: Sorts the GridView control based on the specified sort expression and direction
UpdateRow: Updates the record at the specified row index using the field values of the row
DeleteRow: Deletes the record at the specified index from the data source
IsBindableType: Determines whether the specified data type can be bound to a column in a GridView control
Sort: Sorts the GridView control based on the specified sort expression and direction
UpdateRow: Updates the record at the specified row index using the field values of the row
Events of the GridView Class
PagelndexChanged: Invoked when one of the pager buttons is clicked after the GridView control handles the paging operation
PagelndexChanging: Invoked when one of the pager buttons is clicked, but before the GridView control handles the paging operation
RowCancelingEdit: Invoked when the Cancel button of a row in edit mode is clicked, but before the row exits the edit mode
RowCommand: Invoked when a button is clicked in a GridView control.
RowCreated: Invoked when a row is created in a GridView control
RowDataBound: Invoked when a data row is bound to data in a GridView control
RowDeleted: Invoked when a row's Delete button is clicked, but after the GridView control deletes the row
RowDeleting: Invoked when a row's Delete button is clicked, but before the GridView control deletes the row
RowEditing: Invoked when a row's Edit button is clicked, but before the GridView control enters edit mode
RowUpdated: Invoked when a row's Update button is clicked, but after the GridView control updates the row
RowUpdating: Invoked when a row's Update button is clicked, but before the GridView control updates the row
SelectedlndexChanged: Invoked when a row's Select button is clicked, but after the GridView control handles the select operation
SelectedlndexChanging: Invoked when a row's Select button is clicked, but before the GridView control handles the select operation.
Sorted: Invoked when the hvperlink to sort a column is clicked, but after the GridView control handles the sort operation
Sorting: Invoked when the hyperlink to sort a column is clicked, but before the GridView control handles the sort operation