System.Web.UI.WebControls.DataGrid Class

A data bound list control that displays the items from data source in a table. The System.Web.UI.WebControls.DataGrid control allows you to select, sort, and edit these items.

See Also: DataGrid Members

Syntax

[System.ComponentModel.Designer("System.Web.UI.Design.WebControls.DataGridDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.ComponentModel.Design.IDesigner")]
[System.ComponentModel.Editor("System.Web.UI.Design.WebControls.DataGridComponentEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(System.ComponentModel.ComponentEditor))]
public class DataGrid : BaseDataList, System.Web.UI.INamingContainer

Remarks

In this topic:

Introduction

Use the System.Web.UI.WebControls.DataGrid control to display the fields of a data source as columns in a table. Each row in the System.Web.UI.WebControls.DataGrid control represents a record in the data source. The System.Web.UI.WebControls.DataGrid control supports selection, editing, deleting, paging, and sorting.

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. 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 [<topic://cpconASPNETSyntaxForValidationControls>].

Different column types determine the behavior of the columns in the control. The following table lists the different column types that can be used.

System.Web.UI.WebControls.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 System.Web.UI.WebControls.DataGrid control.

System.Web.UI.WebControls.ButtonColumn

Displays a command button for each item in the column. This allows you to create a column of custom button controls, such as Add or Remove buttons.

System.Web.UI.WebControls.EditCommandColumn

Displays a column that contains editing commands for each item in the column.

System.Web.UI.WebControls.HyperLinkColumn

Displays the contents of each item in the column as a hyperlink. The contents of the column can be bound to a field in a data source or static text.

System.Web.UI.WebControls.TemplateColumn

Displays each item in the column following a specified template. This allows you to provide custom controls in the column.

By default, the DataGrid.AutoGenerateColumns property is set to true, which creates a System.Web.UI.WebControls.BoundColumn object for each field in the data source. Each field is then rendered as a column in the System.Web.UI.WebControls.DataGrid control in the order that each field appears in the data source.

You can also manually control which columns appear in the System.Web.UI.WebControls.DataGrid control by setting the DataGrid.AutoGenerateColumns property to false and then listing the columns that you want to include between the opening and closing <Columns> tags. The columns specified are added to the DataGrid.Columns collection in the order listed. This allows you to programmatically control the columns in the System.Web.UI.WebControls.DataGrid control.

Note:

The order that the columns are displayed in the System.Web.UI.WebControls.DataGrid control is controlled by the order that the columns appear in the DataGrid.Columns collection. Although you can programmatically change the order of the columns by manipulating the DataGrid.Columns collection, it is easier to list the columns in the desired display order.

Explicitly declared columns can be displayed in conjunction with automatically generated columns. When using both, explicitly declared columns will be rendered first, followed by the automatically generated columns.

Note:

Automatically generated columns are not added to the DataGrid.Columns collection.

The appearance of the System.Web.UI.WebControls.DataGrid control may be customized by setting the style properties for the different parts of the control. The following table lists the different style properties.

DataGrid.AlternatingItemStyle

Specifies the style for alternating items in the System.Web.UI.WebControls.DataGrid control.

DataGrid.EditItemStyle

Specifies the style for the item being edited in the System.Web.UI.WebControls.DataGrid control.

DataGrid.FooterStyle

Specifies the style for the footer section in the System.Web.UI.WebControls.DataGrid control.

DataGrid.HeaderStyle

Specifies the style for the header section in the System.Web.UI.WebControls.DataGrid control.

DataGrid.ItemStyle

Specifies the style for the items in the System.Web.UI.WebControls.DataGrid control.

DataGrid.PagerStyle

Specifies the style for the page selection section of the System.Web.UI.WebControls.DataGrid control.

DataGrid.SelectedItemStyle

Specifies the style for the selected item in the System.Web.UI.WebControls.DataGrid 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.

DataGrid.ShowFooter

Shows or hides the footer section of the System.Web.UI.WebControls.DataGrid control.

DataGrid.ShowHeader

Shows or hides the header section of the System.Web.UI.WebControls.DataGrid control.

You can control the appearance of the System.Web.UI.WebControls.DataGrid control by programmatically adding attributes to the <td> and <tr> tags rendered by the control on the browser. Attributes can be programmatically added by providing code in the event handler for the DataGrid.OnItemCreated(DataGridItemEventArgs) or DataGrid.OnItemDataBound(DataGridItemEventArgs) event.

To add an attribute to the <td> tag, first get the System.Web.UI.WebControls.TableCell object that represents the cell in the System.Web.UI.WebControls.DataGrid control you want to add the attribute to. The System.Web.UI.Control.Controls collection for the DataGridItemEventArgs.Item property of the System.Web.UI.WebControls.DataGridItemEventArgs object passed into the event handler can be used to get the desired System.Web.UI.WebControls.TableCell object. You can then use the System.Web.UI.AttributeCollection.Add(string, string) method of the WebControl.Attributes collection for the System.Web.UI.WebControls.TableCell object to add attributes to the <td> tag.

To add an attribute to the <tr> tag, first get the System.Web.UI.WebControls.DataGridItem object that represents the row in the System.Web.UI.WebControls.DataGrid control you want to add the attribute to. The DataGridItemEventArgs.Item property of the System.Web.UI.WebControls.DataGridItemEventArgs object passed into the event handler can be used to get the desired System.Web.UI.WebControls.DataGridItem object. You can then use the System.Web.UI.AttributeCollection.Add(string, string) method of the WebControl.Attributes collection for the System.Web.UI.WebControls.DataGridItem object to add attributes to the <tr> tag.

Accessibility

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.

Declarative Syntax

Example

<asp:DataGrid
    AccessKey="string"
    AllowCustomPaging="True|

Requirements

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in System.Web.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0