private void InsertItem(int itemIndex, object dataItem, bool selectItem)
        {
            // check if the current page is full
            IOSList rl = DataSource;

            // update the datasource
            rl.Insert(dataItem, itemIndex);

            // increase higher item indexes in the iterator
            foreach (IIteratorItem otherItem in Controls)
            {
                if (otherItem.ItemIndex >= itemIndex)
                {
                    otherItem.ItemIndex++;
                }
            }

            // create an item at the end of the list (and don't databind...)
            CreateItem(dataItem, itemIndex, false);

            // update item count
            ViewStateAttributes.SetInViewState("NumItems", ItemCount, null);

            // update item ids and indexes in the viewstate
            StoreViewStateItemIdsandIndexes();

            var previousSelectedIndex   = SelectedIndex;
            var previousDataSourceIndex = rl.CurrentRowNumber;

            SelectedIndex = itemIndex;

            // set the record list position
            rl.SetPosition(itemIndex);

            // databind the created item
            DataBindLastCreatedItem();

            if (!selectItem)
            {
                // revert the record list position
                rl.SetPosition(previousDataSourceIndex);
                SelectedIndex = previousSelectedIndex;
            }
        }
        /// <summary>
        /// Inserts an item to the datagrid
        /// </summary>
        /// <param name="param name="dataItemIndex"></param>
        /// <param name="dataItem"></param>
        public void InsertItem(int dataItemIndex, object dataItem)
        {
            // check if the current page is full
            IOSList rl = DataSource;

            // update the datasource
            rl.Insert(dataItem, dataItemIndex);

            // set the record list position and databind the item
            rl.SetPosition(dataItemIndex);

            // create an item at the end of the list
            OSDataGridItem        item = (OSDataGridItem)CreateItem(dataItemIndex, dataItemIndex, ListItemType.Item);
            DataGridItemEventArgs e    = new DataGridItemEventArgs(item);

            // initialize item
            item.DataItem = dataItem;
            InitializeItem(item, true);
            this.OnItemCreated(e);

            OSDataGridTable child = (OSDataGridTable)Controls[0];

            // remove any empty message row...
            if (((OSDataGridItem)child.Rows[child.Rows.Count - 1]).IsEmptyMessageItem)
            {
                child.Rows.RemoveAt(child.Rows.Count - 1);
            }

            // insert the item before the footer and pager items
            int footerIndex = child.Rows.Count - 2;

            int footerId;

            if (child.Rows[footerIndex].ID != null)
            {
                footerId = OSDataGridItem.GetIntegerId(child.Rows[footerIndex].ID, EnableLegacyRendering);
            }
            else
            {
                footerId = footerIndex + 1;
            }

            child.Rows.AddAt(footerIndex, item);

            // generate a new item id for this control
            int id = footerId;

            item.ID = OSDataGridItem.FormatId(id, EnableLegacyRendering);

            // increase the footer and pager items, so that no repateated ids exist in the page
            child.Rows[child.Rows.Count - 2].ID = OSDataGridItem.FormatId(id + 1, EnableLegacyRendering);
            child.Rows[child.Rows.Count - 1].ID = OSDataGridItem.FormatId(id + 2, EnableLegacyRendering);

            ViewState["_!ItemCount"] = rl.Length;
            _isEmpty = false;

            // increase higher item indexes in the data grid
            foreach (OSDataGridItem otherItem in child.Rows)
            {
                if (otherItem.ItemIndex >= dataItemIndex && otherItem != item)
                {
                    otherItem.SetItemIndex(otherItem.ItemIndex + 1);
                }
            }

            // update row ids in the viewstate
            StoreViewStateRowIdsandItemIndexes();

            SelectedIndex = dataItemIndex;

            item.DataBind();
            this.OnItemDataBound(e);

            item.DataItem = null;
        }