System.Web.UI.WebControls (system.web.dll)class

The DataGrid control is, at its simplest, a data-bound list displayed in a table grid structure. It provides a rich set of functionality that makes it the most versatile data-bound control, including support for selection, editing, deleting, paging, and sorting.

Columns can be added to a DataGrid in two ways. First, if the AutoGenerateColumns property is True, a BoundColumn will be created for every column in the data source specified by DataSource. Alternatively, you can define columns by adding nested tags in the .aspx file. (If you mix both approaches, the automatically generated columns will always be added last.) Columns can be BoundColumn, ButtonColumn, EditCommandColumn, HyperLinkColumn, or TemplateColumn controls (each of which are described separately in this namespace). The order that the columns appear is determined by the order the column tags are listed in the .aspx, and you can manipulate them programmatically through the Columns collection. Note that this collection does not contain automatically generated columnsonly ones that have defined templates.

The DataGrid has a number of properties that allow you to control its appearance. You can set TableItemStyle objects for various properties, including footers, headers (used automatically for column titles), and items. (The corresponding DataGrid properties end with the word "Style.") You can also use the ShowHeader and ShowFooter properties to configure whether headers and footers will be displayed.

To allow row selection for DataGrid, set the SelectedItemTemplate to look different than the ItemTemplate. Then add a button column that allows selection (for example, you might use the text "Select") and set the SelectedIndex property to the appropriate row in the ItemCommand event handler. To allow in-place editing, add an EditCommandColumn column and set the EditItemIndex (then rebind to the data source) in the EditCommand event handler. Any properties that are not marked as read-only in the template will be editable through automatically provided text boxes. You can handle the UpdateCommand event to commit the actual change. To disable editing or selection for a DataGrid, set the EditItemIndex or SelectedIndex to -1.

To provide sorting, enable the AllowSorting property and rebind the appropriate sorted data in response to the SortCommand event. Finally, to provide paging, enable the AllowPaging property and set a number of rows in the PageSize property. When the PageIndexChanged event is triggered, set the CurrentPageIndex to the appropriate page. Note that automatic paging causes the complete data table to be retrieved, even though only a few rows are being displayed. To optimize performance, you should enable the AllowCustomPaging property and provide custom data access code in the CurrentPageIndex event handler. When using custom paging, you must also set the VirtualItemCount property to the total number of records to allow the DataGrid to determine the total number of pages needed.

public class DataGrid : BaseDataList, System.Web.UI.INamingContainer {
// Public Constructors
   public DataGrid( );
// Public Static Fields
   public const string CancelCommandName;           // =Cancel
   public const string DeleteCommandName;           // =Delete
   public const string EditCommandName;             // =Edit
   public const string NextPageCommandArgument;                  // =Next
   public const string PageCommandName;             // =Page
   public const string PrevPageCommandArgument;                  // =Prev
   public const string SelectCommandName;           // =Select
   public const string SortCommandName;             // =Sort
   public const string UpdateCommandName;           // =Update
// Public Instance Properties
   public virtual bool AllowCustomPaging{set; get; }
   public virtual bool AllowPaging{set; get; }
   public virtual bool AllowSorting{set; get; }
   public virtual TableItemStyle AlternatingItemStyle{get; }
   public virtual bool AutoGenerateColumns{set; get; }
   public virtual string BackImageUrl{set; get; }
   public virtual DataGridColumnCollection Columns{get; }
   public int CurrentPageIndex{set; get; }
   public virtual int EditItemIndex{set; get; }
   public virtual TableItemStyle EditItemStyle{get; }
   public virtual TableItemStyle FooterStyle{get; }
   public virtual TableItemStyle HeaderStyle{get; }
   public virtual DataGridItemCollection Items{get; }
   public virtual TableItemStyle ItemStyle{get; }
   public int PageCount{get; }
   public virtual DataGridPagerStyle PagerStyle{get; }
   public virtual int PageSize{set; get; }
   public virtual int SelectedIndex{set; get; }
   public virtual DataGridItem SelectedItem{get; }
   public virtual TableItemStyle SelectedItemStyle{get; }
   public virtual bool ShowFooter{set; get; }
   public virtual bool ShowHeader{set; get; }
   public virtual int VirtualItemCount{set; get; }
// Protected Instance Methods
   protected virtual ArrayList CreateColumnSet(PagedDataSource dataSource, bool useDataSource);
   protected override void CreateControlHierarchy(bool useDataSource);   // overrides BaseDataList
   protected override Style CreateControlStyle( );                // overrides WebControl
   protected virtual DataGridItem CreateItem(int itemIndex, int dataSourceIndex, ListItemType itemType);
   protected virtual void InitializeItem(DataGridItem item, DataGridColumn[ ] columns);
   protected virtual void InitializePager(DataGridItem item, int columnSpan, PagedDataSource pagedDataSource);
   protected override void LoadViewState(object savedState);    // overrides WebControl
   protected override bool OnBubbleEvent(object source, EventArgs e);// overrides System.Web.UI.Control
   protected virtual void OnCancelCommand(DataGridCommandEventArgs e);
   protected virtual void OnDeleteCommand(DataGridCommandEventArgs e);
   protected virtual void OnEditCommand(DataGridCommandEventArgs e);
   protected virtual void OnItemCommand(DataGridCommandEventArgs e);
   protected virtual void OnItemCreated(DataGridItemEventArgs e);
   protected virtual void OnItemDataBound(DataGridItemEventArgs e);
   protected virtual void OnPageIndexChanged(DataGridPageChangedEventArgs e);
   protected virtual void OnSortCommand(DataGridSortCommandEventArgs e);
   protected virtual void OnUpdateCommand(DataGridCommandEventArgs e);
   protected override void PrepareControlHierarchy( ); // overrides BaseDataList
   protected override object SaveViewState( ); // overrides WebControl
   protected override void TrackViewState( );// overrides WebControl
// Events
   public event DataGridCommandEventHandler CancelCommand;
   public event DataGridCommandEventHandler DeleteCommand;
   public event DataGridCommandEventHandler EditCommand;
   public event DataGridCommandEventHandler ItemCommand;
   public event DataGridItemEventHandler ItemCreated;
   public event DataGridItemEventHandler ItemDataBound;
   public event DataGridPageChangedEventHandler PageIndexChanged;
   public event DataGridSortCommandEventHandler SortCommand;
   public event DataGridCommandEventHandler UpdateCommand;


System.Object System.Web.UI.Control(System.ComponentModel.IComponent, System.IDisposable, System.Web.UI.IParserAccessor, System.Web.UI.IDataBindingsAccessor) WebControl(System.Web.UI.IAttributeAccessor) BaseDataList DataGrid(System.Web.UI.INamingContainer)

Returned By


Passed To

DataGridColumnCollection.DataGridColumnCollection( )

    Part I: Introduction to ASP.NET
    Part III: Namespace Reference
    Chapter 40. The System.Web.UI.MobileControls Namespace
    Chapter 42. The System.Web.UI.WebControls Namespace