关于asp .net datagrid

原文链接: http://www.cnblogs.com/huangwj21/archive/2011/06/10/2077654.html

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 datagriditem has itemtype 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 should 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 should let your datagrid have your own datagriditem, then you can use it do whatever you want, you should 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 many times as a datagrid's datagriditem which's type is item or alternatingitem)

6.datagrid unload()

转载于:https://www.cnblogs.com/huangwj21/archive/2011/06/10/2077654.html

猜你喜欢

转载自blog.csdn.net/weixin_30387799/article/details/94963604
今日推荐