asp.net.ph

DataGrid Class

System.Web.UI.WebControls Namespace


A data bound list control that displays the records from a data source in a tabular grid.

DataGrid Class Members

Collapse   Constructors

Visibility Constructor Parameters
public DataGrid ( )

Collapse   Fields

Visibility Name Type
public static const CancelCommandName String
public static const DeleteCommandName String
public static const EditCommandName String
public static const NextPageCommandArgument String
public static const PageCommandName String
public static const PrevPageCommandArgument String
public static const SelectCommandName String
public static const SortCommandName String
public static const UpdateCommandName String

Collapse   Properties

Visibility Name Value Type Accessibility
public AllowCustomPaging Boolean [ Get , Set ]
public AllowPaging Boolean [ Get , Set ]
public AllowSorting Boolean [ Get , Set ]
public AlternatingItemStyle TableItemStyle [ Get ]
public AutoGenerateColumns Boolean [ Get , Set ]
public BackImageUrl String [ Get , Set ]
public Columns DataGridColumnCollection [ Get ]
public CurrentPageIndex Int32 [ Get , Set ]
public EditItemIndex Int32 [ Get , Set ]
public EditItemStyle TableItemStyle [ Get ]
public FooterStyle TableItemStyle [ Get ]
public HeaderStyle TableItemStyle [ Get ]
public Items DataGridItemCollection [ Get ]
public ItemStyle TableItemStyle [ Get ]
public PageCount Int32 [ Get ]
public PagerStyle DataGridPagerStyle [ Get ]
public PageSize Int32 [ Get , Set ]
public SelectedIndex Int32 [ Get , Set ]
public SelectedItem DataGridItem [ Get ]
public SelectedItemStyle TableItemStyle [ Get ]
public ShowFooter Boolean [ Get , Set ]
public ShowHeader Boolean [ Get , Set ]
public VirtualItemCount Int32 [ Get , Set ]

Collapse   Methods

Visibility Name Parameters Return Type
protected CreateColumnSet ( PagedDataSource dataSource , Boolean useDataSource ) ArrayList
protected CreateControlHierarchy ( Boolean useDataSource ) Void
protected CreateControlStyle ( ) Style
protected CreateItem ( Int32 itemIndex , Int32 dataSourceIndex , ListItemType itemType ) DataGridItem
protected InitializeItem ( DataGridItem item , DataGridColumn columns ) Void
protected InitializePager ( DataGridItem item , Int32 columnSpan , PagedDataSource pagedDataSource ) Void
protected LoadViewState ( Object savedState ) Void
protected OnBubbleEvent ( Object source , EventArgs e ) Boolean
protected OnCancelCommand ( DataGridCommandEventArgs e ) Void
protected OnDeleteCommand ( DataGridCommandEventArgs e ) Void
protected OnEditCommand ( DataGridCommandEventArgs e ) Void
protected OnItemCommand ( DataGridCommandEventArgs e ) Void
protected OnItemCreated ( DataGridItemEventArgs e ) Void
protected OnItemDataBound ( DataGridItemEventArgs e ) Void
protected OnPageIndexChanged ( DataGridPageChangedEventArgs e ) Void
protected OnSortCommand ( DataGridSortCommandEventArgs e ) Void
protected OnUpdateCommand ( DataGridCommandEventArgs e ) Void
protected SaveViewState ( ) Object
protected TrackViewState ( ) Void

Remarks

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

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

Column Type Description
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 DataGrid control.
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.
EditCommandColumn Displays a column that contains editing commands for each item in the column.
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.
TemplateColumn Displays each item in the column following a specified template. This allows you to provide custom controls in the column.

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

You can also manually control which columns appear in the DataGrid control by setting the 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 Columns collection in the order listed. This allows you to programmatically control the columns in the DataGrid control.

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

NOTE: Explicitly declared columns may be used in conjunction with automatically generated columns. When using both, explicitly declared columns will be rendered first, followed by the automatically generated columns. Automatically generated columns are not added to the Columns collection.

The appearance of the 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.

Style Property Description
AlternatingItemStyle Specifies the style for alternating items in the DataGrid control.
EditItemStyle Specifies the style for the item being edited in the DataGrid control.
FooterStyle Specifies the style for the footer section in the DataGrid control.
HeaderStyle Specifies the style for the header section in the DataGrid control.
ItemStyle Specifies the style for the items in the DataGrid control.
PagerStyle Specifies the style for the page selection section of the DataGrid control.
SelectedItemStyle Specifies the style for the selected item in the 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.

Property Description
ShowFooter Shows or hides the footer section of the DataGrid control.
ShowHeader Shows or hides the header section of the DataGrid control.

You can also control the appearance of the 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 OnItemCreated or OnItemDataBound event.

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

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

See Also

BaseDataList   DataList   Repeater   BoundColumn   ButtonColumn   EditCommandColumn   HyperLinkColumn   TemplateColumn Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph