System.Web.UI.WebControls Namespace
.NET Framework version 2.0
Displays the values of a data source in a table where each column represents a field and each row represents a record. The GridView control allows you to select, sort, and edit these rows.
Visibility |
Constructor |
Parameters |
public |
GridView |
( )
|
Visibility |
Name |
Value Type |
Accessibility |
public |
AllowCustomPaging
|
Boolean |
[ Get , Set ] |
public |
AllowPaging
|
Boolean |
[ Get , Set ] |
public |
AllowSorting
|
Boolean |
[ Get , Set ] |
public |
AlternatingRowStyle
|
TableItemStyle |
[ Get ] |
public |
AutoGenerateColumns
|
Boolean |
[ Get , Set ] |
public |
AutoGenerateDeleteButton
|
Boolean |
[ Get , Set ] |
public |
AutoGenerateEditButton
|
Boolean |
[ Get , Set ] |
public |
AutoGenerateSelectButton
|
Boolean |
[ Get , Set ] |
public |
BackImageUrl
|
String |
[ Get , Set ] |
public |
BottomPagerRow
|
GridViewRow |
[ Get ] |
public |
Caption
|
String |
[ Get , Set ] |
public |
CaptionAlign
|
TableCaptionAlign |
[ Get , Set ] |
public |
CellPadding
|
Int32 |
[ Get , Set ] |
public |
CellSpacing
|
Int32 |
[ Get , Set ] |
public |
ClientIDRowSuffix
|
String |
[ Get , Set ] |
public |
ClientIDRowSuffixDataKeys
|
DataKeyArray |
[ Get ] |
public |
Columns
|
DataControlFieldCollection |
[ Get ] |
public |
ColumnsGenerator
|
IAutoFieldGenerator |
[ Get , Set ] |
public |
DataKeyNames
|
String |
[ Get , Set ] |
public |
DataKeys
|
DataKeyArray |
[ Get ] |
public |
DeleteMethod
|
String |
[ Get , Set ] |
public |
EditIndex
|
Int32 |
[ Get , Set ] |
public |
EditRowStyle
|
TableItemStyle |
[ Get ] |
public |
EmptyDataRowStyle
|
TableItemStyle |
[ Get ] |
public |
EmptyDataTemplate
|
ITemplate |
[ Get , Set ] |
public |
EmptyDataText
|
String |
[ Get , Set ] |
public |
EnableModelValidation
|
Boolean |
[ Get , Set ] |
public |
EnablePersistedSelection
|
Boolean |
[ Get , Set ] |
public |
EnableSortingAndPagingCallbacks
|
Boolean |
[ Get , Set ] |
public |
FooterRow
|
GridViewRow |
[ Get ] |
public |
FooterStyle
|
TableItemStyle |
[ Get ] |
public |
GridLines
|
GridLines |
[ Get , Set ] |
public |
HeaderRow
|
GridViewRow |
[ Get ] |
public |
HeaderStyle
|
TableItemStyle |
[ Get ] |
public |
HorizontalAlign
|
HorizontalAlign |
[ Get , Set ] |
public |
PageCount
|
Int32 |
[ Get ] |
public |
PageIndex
|
Int32 |
[ Get , Set ] |
public |
PagerSettings
|
PagerSettings |
[ Get ] |
public |
PagerStyle
|
TableItemStyle |
[ Get ] |
public |
PagerTemplate
|
ITemplate |
[ Get , Set ] |
public |
PageSize
|
Int32 |
[ Get , Set ] |
public |
RowHeaderColumn
|
String |
[ Get , Set ] |
public |
Rows
|
GridViewRowCollection |
[ Get ] |
public |
RowStyle
|
TableItemStyle |
[ Get ] |
public |
SelectedDataKey
|
DataKey |
[ Get ] |
public |
SelectedIndex
|
Int32 |
[ Get , Set ] |
public |
SelectedPersistedDataKey
|
DataKey |
[ Get , Set ] |
public |
SelectedRow
|
GridViewRow |
[ Get ] |
public |
SelectedRowStyle
|
TableItemStyle |
[ Get ] |
public |
SelectedValue
|
Object |
[ Get ] |
public |
ShowFooter
|
Boolean |
[ Get , Set ] |
public |
ShowHeader
|
Boolean |
[ Get , Set ] |
public |
ShowHeaderWhenEmpty
|
Boolean |
[ Get , Set ] |
public |
SortDirection
|
SortDirection |
[ Get ] |
public |
SortedAscendingCellStyle
|
TableItemStyle |
[ Get ] |
public |
SortedAscendingHeaderStyle
|
TableItemStyle |
[ Get ] |
public |
SortedDescendingCellStyle
|
TableItemStyle |
[ Get ] |
public |
SortedDescendingHeaderStyle
|
TableItemStyle |
[ Get ] |
public |
SortExpression
|
String |
[ Get ] |
public |
TopPagerRow
|
GridViewRow |
[ Get ] |
public |
UpdateMethod
|
String |
[ Get , Set ] |
public |
UseAccessibleHeader
|
Boolean |
[ Get , Set ] |
public |
VirtualItemCount
|
Int32 |
[ Get , Set ] |
Visibility |
Name |
Parameters |
Return Type |
protected |
CreateAutoGeneratedColumn |
(
AutoGeneratedFieldProperties
fieldProperties
)
|
AutoGeneratedField
|
protected |
CreateChildControls |
(
IEnumerable
dataSource
,
Boolean
dataBinding
)
|
Int32
|
protected |
CreateChildTable |
( )
|
Table
|
protected |
CreateColumns |
(
PagedDataSource
dataSource
,
Boolean
useDataSource
)
|
ICollection
|
protected |
CreateControlStyle |
( )
|
Style
|
protected |
CreateDataSourceSelectArguments |
( )
|
DataSourceSelectArguments
|
protected |
CreateRow |
(
Int32
rowIndex
,
Int32
dataSourceIndex
,
DataControlRowType
rowType
,
DataControlRowState
rowState
)
|
GridViewRow
|
public |
DataBind |
( )
|
Void
|
public |
DeleteRow |
(
Int32
rowIndex
)
|
Void
|
protected |
ExtractRowValues |
(
IOrderedDictionary
fieldValues
,
GridViewRow
row
,
Boolean
includeReadOnlyFields
,
Boolean
includePrimaryKey
)
|
Void
|
protected |
GetCallbackResult |
( )
|
String
|
protected |
GetCallbackScript |
(
IButtonControl
buttonControl
,
String
argument
)
|
String
|
protected |
InitializePager |
(
GridViewRow
row
,
Int32
columnSpan
,
PagedDataSource
pagedDataSource
)
|
Void
|
protected |
InitializeRow |
(
GridViewRow
row
,
DataControlField
fields
)
|
Void
|
public |
IsBindableType |
(
Type
type
)
|
Boolean
|
protected |
LoadViewState |
(
Object
savedState
)
|
Void
|
protected |
OnBubbleEvent |
(
Object
source
,
EventArgs
e
)
|
Boolean
|
protected |
OnDataPropertyChanged |
( )
|
Void
|
protected |
OnDataSourceViewChanged |
(
Object
sender
,
EventArgs
e
)
|
Void
|
protected |
OnPageIndexChanged |
(
EventArgs
e
)
|
Void
|
protected |
OnPageIndexChanging |
(
GridViewPageEventArgs
e
)
|
Void
|
protected |
OnPagePreLoad |
(
Object
sender
,
EventArgs
e
)
|
Void
|
protected |
OnRowCancelingEdit |
(
GridViewCancelEditEventArgs
e
)
|
Void
|
protected |
OnRowCommand |
(
GridViewCommandEventArgs
e
)
|
Void
|
protected |
OnRowCreated |
(
GridViewRowEventArgs
e
)
|
Void
|
protected |
OnRowDataBound |
(
GridViewRowEventArgs
e
)
|
Void
|
protected |
OnRowDeleted |
(
GridViewDeletedEventArgs
e
)
|
Void
|
protected |
OnRowDeleting |
(
GridViewDeleteEventArgs
e
)
|
Void
|
protected |
OnRowEditing |
(
GridViewEditEventArgs
e
)
|
Void
|
protected |
OnRowUpdated |
(
GridViewUpdatedEventArgs
e
)
|
Void
|
protected |
OnRowUpdating |
(
GridViewUpdateEventArgs
e
)
|
Void
|
protected |
OnSelectedIndexChanged |
(
EventArgs
e
)
|
Void
|
protected |
OnSelectedIndexChanging |
(
GridViewSelectEventArgs
e
)
|
Void
|
protected |
OnSorted |
(
EventArgs
e
)
|
Void
|
protected |
OnSorting |
(
GridViewSortEventArgs
e
)
|
Void
|
protected |
RaiseCallbackEvent |
(
String
eventArgument
)
|
Void
|
protected |
RaisePostBackEvent |
(
String
eventArgument
)
|
Void
|
protected |
SaveViewState |
( )
|
Object
|
public |
SelectRow |
(
Int32
rowIndex
)
|
Void
|
public |
SetEditRow |
(
Int32
rowIndex
)
|
Void
|
public |
SetPageIndex |
(
Int32
rowIndex
)
|
Void
|
public |
Sort |
(
String
sortExpression
,
SortDirection
sortDirection
)
|
Void
|
protected |
TrackViewState |
( )
|
Void
|
public |
UpdateRow |
(
Int32
rowIndex
,
Boolean
causesValidation
)
|
Void
|
|
The 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 GridView control supports the following features:
- Binding to data source controls, such as SqlDataSource.
- Built-in sorting capabilities.
- Built-in updating and deleting capabilities.
- Built-in paging capabilities.
- Built-in row selection capabilities.
- Programmatic access to the 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.
NOTE: If you are familiar with the DataGrid control from the .NET Framework version 1.0, the GridView control is the successor to the DataGrid control.
Each column in the GridView control is represented by a DataControlField object. By default, the AutoGenerateColumns property is set to true, which creates an AutoGeneratedField object for each field in the data source. Each field is then rendered as a column in the GridView control in the order that each field appears in the data source.
You can also manually control which column fields appear in the GridView control by setting the 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.
Column field type |
Description |
BoundField |
Displays the value of a field in a data source. This is the default column type of the GridView control. |
ButtonField |
Displays a command button for each item in the GridView control. This allows you to create a column of custom button controls, such as the Add or the Remove button. |
CheckBoxField |
Displays a check box for each item in the GridView control. This column field type is commonly used to display fields with a Boolean value. |
CommandField |
Displays predefined command buttons to perform selecting, editing, or deleting operations. |
HyperLinkField |
Displays 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 |
Displays an image for each item in the GridView control. |
TemplateField |
Displays user-defined content for each item in the GridView control according to a specified template. This column field type allows 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 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 Columns collection in the order listed. The Columns collection stores all the column fields in the control and allows you to programmatically manage the column fields in the 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.
NOTE: Automatically generated column fields are not added to the Columns collection.
The GridView control can be bound to a data source control ( such as SqlDataSource, ObjectDataSource, and so on ), as well as any data source that implements the System.Collections.IEnumerable interface ( such as System.Data.DataView, System.Collections.ArrayList, or System.Collections.Hashtable ). Use one of the following methods to bind the GridView control to the appropriate data source type:
- To bind to a data source control, set the DataSourceID property of the GridView control to the ID value of the data source control. The 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 functionality. This is the preferred method to bind to data.
- To bind to a data source that implements the System.Collections.IEnumerable interface, programmatically set the DataSource property of the GridView control to the data source and then call the DataBind method. When using this method, the GridView control does not provide built-in sorting, updating, deleting, and paging functionality. You need to provide this functionality by using the appropriate event.
NOTE: 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 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 Validating User Input in Web Forms.
The 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 GridView control is bound to a data source control, the GridView control can take advantage of the data source control's capabilities and provide automatic sorting, updating, and deleting functionality.
NOTE: The 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 GridView control with respect to a specific column by clicking on the column's header. To enable sorting, set the AllowSorting property to true.
The automatic updating, deleting, and selection functionalities are enabled when a button in a ButtonField or TemplateField column field, with a command name of "Edit", "Delete", and "Select", respectively, is clicked. The GridView control can automatically add a CommandField field with an Edit, Delete, or Select button if the AutoGenerateEditButton, AutoGenerateDeleteButton, or AutoGenerateSelectButton property is set to true, respectively.
NOTE: Inserting records into the data source is not directly supported by the GridView control. However, it is possible to insert records by using the GridView control in conjunction with the DetailsView or FormView control. For more information, see DetailsView or FormView, respectively.
Instead of displaying all the records in the data source at the same time, the GridView control can automatically break the records up into pages. To enable paging, set the AllowPaging property to true.
You can customize the appearance of the GridView control by setting the style properties for the different parts of the control. The following table lists the different style properties.
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 row being edited in the GridView control. |
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. |
SelectedRowStyle |
The style settings for the selected row in the GridView control. |
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.
Property |
Description |
ShowFooter |
Shows or hides the footer section of the GridView control. |
ShowHeader |
Shows or hides the header section of the GridView control. |
For examples illustrating use of this control, see the individual member types of this class. For syntax information, see GridView in ASP.NET Syntax for Web Controls.
GridView Web Server Control