About asp. net datagrid

Source: Internet
Author: User

The events of life circle in a datagrid

First, there is a important object in datagrid: datagriditem, we can get them use datagrid. items which is inherited from System. Web. UI. WebControls;

The maid with type of enum listitemtype and include header, pager, footer, item, alternatingItem, SelectedItem, EditItem, Separator

Itemtype "item and alternatingItem" are very important cause we can reterive data from item with these two types which is bounding to the datasource.

At the same time, we can let a datagrid has our own datagriditems:

We shoshould have a class which is inherited from System. Web. UI. WebControls. DataGridItem:

public class DataGridItem : System.Web.UI.WebControls.DataGridItem, IPostBackEventHandler    {        public DataGridItem(int itemIndex, int dataSetIndex, ListItemType itemType)            : base(itemIndex, dataSetIndex, itemType)        {        }        #region IPostBackEventHandler Members        public void RaisePostBackEvent(string eventArgument)        {            var commandArgs = new CommandEventArgs(eventArgument, null);            var args =                new DataGridCommandEventArgs(this, this, commandArgs);            RaiseBubbleEvent(this, args);        }
    public overrid void Render(HtmlTextWriter writer)
   {
        // code here can change the style of a datagrid.
   }
       #endregion    }

If you override the render method, we can change the style of a datagrid.

Don't forget to use this DataGridItem, you shocould let your datagrid have your own datagriditem, then you can use it do whatever you want, you shoshould override onitemcreate method in your datagrid


protected override System.Web.UI.WebControls.DataGridItem CreateItem(int itemIndex, int dataSourceIndex, ListItemType itemType)        {            switch (itemType)            {                case ListItemType.SelectedItem:                case ListItemType.AlternatingItem:                case ListItemType.Item:                return new DataGridItem(itemIndex, dataSourceIndex, itemType, new RMAFormPickupDetail(DataSet.CurrentTable.Rows[getDataSetIndex(dataSourceIndex)]));                default:                    return base.CreateItem(itemIndex, dataSourceIndex, itemType);            }        }

Here, your datagrid has your own datagriditem.

The event of a datagrid.

Datagrid. datasource = somedatasource;

Datagrid. databind ();

When this happens, the events happens as following sequence:

1. datagrid createitem ()

2. template control in datagrid load ()

3. template control in datagrid bind ()

4. datagriditem render ()

5. datagrid onitemdatabind () (a datagriditem render () as frequently times as a datagrid's datagriditem which's type is item or alternatingitem)

6. datagrid unload ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.