The DataGrid control renders the items of a given data source into a table. The records and fields in the data source are displayed as rows and columns in the grid.
Declarative Syntax
<asp:DataGrid
AccessKey = "string"
AllowCustomPaging = "True | False"
AllowPaging = "True | False"
AllowSorting = "True | False"
AutoGenerateColumns = "True | False"
BackColor = "color name | #dddddd"
BackImageUrl = "uri"
BorderColor = "color name | #dddddd"
BorderStyle = "NotSet | None | Dotted | Dashed | Solid | Double | Groove |
Ridge | Inset | Outset"
BorderWidth = size
Caption = "string"
CaptionAlign = "NotSet | Top | Bottom | Left | Right"
CellPadding = integer
CellSpacing = integer
CssClass = "string"
DataKeyField = "string"
DataMember = "string"
DataSource = "string"
DataSourceID = "string"
EditItemIndex = integer
Enabled = "True | False"
EnableTheming = "True | False"
EnableViewState = "True | False"
Font-Bold = "True | False"
Font-Italic = "True | False"
Font-Names = "string"
Font-Overline = "True | False"
Font-Size = "string | Smaller | Larger | XX-Small | X-Small | Small |
Medium | Large | X-Large | XX-Large"
Font-Strikeout = "True | False"
Font-Underline = "True | False"
ForeColor = "color name | #dddddd"
GridLines = "None | Horizontal | Vertical | Both"
Height = size
HorizontalAlign = "NotSet | Left | Center | Right | Justify"
ID = "string"
OnCancelCommand = "CancelCommand event handler"
OnDataBinding = "DataBinding event handler"
OnDeleteCommand = "DeleteCommand event handler"
OnDisposed = "Disposed event handler"
OnEditCommand = "EditCommand event handler"
OnInit = "Init event handler"
OnItemCommand = "ItemCommand event handler"
OnItemCreated = "ItemCreated event handler"
OnItemDataBound = "ItemDataBound event handler"
OnLoad = "Load event handler"
OnPageIndexChanged = "PageIndexChanged event handler"
OnPreRender = "PreRender event handler"
OnSelectedIndexChanged = "SelectedIndexChanged event handler"
OnSortCommand = "SortCommand event handler"
OnUnload = "Unload event handler"
OnUpdateCommand = "UpdateCommand event handler"
PageSize = integer
runat = "server"
SelectedIndex = integer
ShowFooter = "True | False"
ShowHeader = "True | False"
SkinID = "string"
Style = "string"
TabIndex = integer
ToolTip = "string"
UseAccessibleHeader = "True | False"
Visible = "True | False"
Width = size
>
<AlternatingItemStyle />
<Columns>
<asp:BoundColumn
DataField = "string"
DataFormatString = "string"
FooterText = "string"
HeaderImageUrl = "uri"
HeaderText = "string"
ReadOnly = "True | False"
SortExpression = "string"
Visible = "True | False"
>
<FooterStyle />
<HeaderStyle />
<ItemStyle />
</asp:BoundColumn>
<asp:ButtonColumn
ButtonType = "LinkButton | PushButton"
CausesValidation = "True | False"
CommandName = "string"
DataTextField = "string"
DataTextFormatString = "string"
FooterText = "string"
HeaderImageUrl = "uri"
HeaderText = "string"
SortExpression = "string"
Text = "string"
ValidationGroup = "string"
Visible = "True | False"
>
<FooterStyle />
<HeaderStyle />
<ItemStyle />
</asp:ButtonColumn>
<asp:EditCommandColumn
ButtonType = "LinkButton | PushButton"
CancelText = "string"
CausesValidation = "True | False"
EditText = "string"
FooterText = "string"
HeaderImageUrl = "uri"
HeaderText = "string"
SortExpression = "string"
UpdateText = "string"
ValidationGroup = "string"
Visible = "True | False"
>
<FooterStyle />
<HeaderStyle />
<ItemStyle />
</asp:EditCommandColumn>
<asp:HyperLinkColumn
DataNavigateUrlField = "string"
DataNavigateUrlFormatString = "string"
DataTextField = "string"
DataTextFormatString = "string"
FooterText = "string"
HeaderImageUrl = "uri"
HeaderText = "string"
NavigateUrl = "uri"
SortExpression = "string"
Target = "string | _blank | _parent | _search | _self | _top"
Text = "string"
Visible = "True | False"
>
<FooterStyle />
<HeaderStyle />
<ItemStyle />
</asp:HyperLinkColumn>
<asp:TemplateColumn
FooterText = "string"
HeaderImageUrl = "uri"
HeaderText = "string"
SortExpression = "string"
Visible = "True | False"
>
<FooterStyle />
<HeaderStyle />
<ItemStyle />
<EditItemTemplate>
<!-- child controls -->
</EditItemTemplate>
<FooterTemplate>
<!-- child controls -->
</FooterTemplate>
<HeaderTemplate>
<!-- child controls -->
</HeaderTemplate>
<ItemTemplate>
<!-- child controls -->
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<EditItemStyle />
<FooterStyle />
<HeaderStyle />
<ItemStyle />
<PagerStyle
BackColor = "color name | #dddddd"
BorderColor = "color name | #dddddd"
BorderStyle = "NotSet | None | Dotted | Dashed | Solid | Double |
Groove | Ridge | Inset | Outset"
BorderWidth = size
CssClass = "string"
Font-Bold = "True | False"
Font-Italic = "True | False"
Font-Names = "string"
Font-Overline = "True | False"
Font-Size = "string | Smaller | Larger | XX-Small | X-Small | Small |
Medium | Large | X-Large | XX-Large"
Font-Strikeout = "True | False"
Font-Underline = "True | False"
ForeColor = "color name | #dddddd"
Height = size
HorizontalAlign = "NotSet | Left | Center | Right | Justify"
Mode = "NextPrev | NumericPages"
NextPageText = "string"
OnDisposed = "Disposed event handler"
PageButtonCount = integer
Position = "Bottom | Top | TopAndBottom"
PrevPageText = "string"
VerticalAlign = "NotSet | Top | Middle | Bottom"
Visible = "True | False"
Width = size
Wrap = "True | False"
/>
<SelectedItemStyle />
</asp:DataGrid>
For information on the individual members of this class, see DataGrid in the class library.
In addition to its default settings, the DataGrid supports a vatiety of options to customize the layout of the grid and add specific functionality. Authors can control which fields are displayed in the DataGrid control and also allow users to select, edit, sort, and page thru the items in the control.
The following sections describe variations of the syntax used for implementing a DataGrid control on a Web Forms page.
The minimum syntax required to implement a DataGrid control is shown below.
<asp:DataGrid runat="server"
id="accessID"
DataSource = '<%# DataBindingExpression %>' />
The DataSource property sets the source to a list of values used to populate the items within the control. This property can also be, and in most cases is, set in code.
For an example of using a basic DataGrid control, see Adding DataGrid Controls to a Web Forms Page.
To modify the default look and layout of the grid ( adjust the color, font, borders, spacing, and so on for the grid as a whole ), authors can declaratively set any of the control's member properties within the opening tag.
<asp:DataGrid runat="server"
id="accessID"
...
propertyName = "propertyValue" />
where propertyName can be any of the following.
| Property |
Description |
| Base properties |
The properties inherited from the base WebControl class. |
| AutoGenerateColumns |
Specifies whether columns will be generated for each bound data field. Set to false when customizing column layout. When not specified, defaults to true. |
| BackImageUrl |
The URL of an image to display behind the control. |
| CellPadding |
The amount of space, in pixels, between the contents of a cell and the cell's border. When programming, this property is set using Units. |
| CellSpacing |
The amount of space, in pixels, between cells. When programming, this property is set using Units. |
| GridLines |
How grid lines are displayed between items. Possible values are None, Horizontal, Vertical, and Both. The default is None. |
| HorizontalAlign |
The alignment of the rows with respect to surrounding text. Possible values are Left, Center, Right, Justified, and NotSet. The default is NotSet. |
| ShowFooter |
true if the footer should be rendered, false otherwise. |
| ShowHeader |
true if the header should be rendered, false otherwise. |