/// <internalonly/>
        /// <devdoc>
        ///    <para>Creates the control hierarchy that is used to render the DataGrid.
        ///       This is called whenever a control hierarchy is needed and the
        ///       ChildControlsCreated property is false.
        ///       The implementation assumes that all the children in the controls
        ///       collection have already been cleared.</para>
        /// </devdoc>
        protected override void CreateControlHierarchy(bool useDataSource) {
            pagedDataSource = CreatePagedDataSource();

            IEnumerator dataSource = null;
            int count = -1;
            int totalCount = -1;
            ArrayList keysArray = DataKeysArray;
            ArrayList columnsArray = null;

            if (itemsArray != null) {
                itemsArray.Clear();
            }
            else {
                itemsArray = new ArrayList();
            }
            itemsCollection = null;

            if (useDataSource == false) {
                // ViewState must have a non-null value for ItemCount because we check for
                // this in CreateChildControls
                count = (int)ViewState[BaseDataList.ItemCountViewStateKey];
                totalCount = (int)ViewState[DataSourceItemCountViewStateKey];

                if (count != -1) {
                    if (pagedDataSource.IsCustomPagingEnabled) {
                        pagedDataSource.DataSource = new DummyDataSource(count);
                    }
                    else {
                        pagedDataSource.DataSource = new DummyDataSource(totalCount);
                    }
                    dataSource = pagedDataSource.GetEnumerator();
                    columnsArray = CreateColumnSet(null, false);

                    itemsArray.Capacity = count;
                }
            }
            else {
                keysArray.Clear();

                IEnumerable realDataSource = GetData();

                if (realDataSource != null) {
                    ICollection collection = realDataSource as ICollection;

                    if ((collection == null) &&
                        pagedDataSource.IsPagingEnabled && !pagedDataSource.IsCustomPagingEnabled) {
                        throw new HttpException(SR.GetString(SR.DataGrid_Missing_VirtualItemCount, ID));
                    }

                    pagedDataSource.DataSource = realDataSource;
                    if (pagedDataSource.IsPagingEnabled) {
                        if ((pagedDataSource.CurrentPageIndex < 0) || (pagedDataSource.CurrentPageIndex >= pagedDataSource.PageCount)) {
                            throw new HttpException(SR.GetString(SR.Invalid_CurrentPageIndex));
                        }
                    }
                    columnsArray = CreateColumnSet(pagedDataSource, useDataSource);

                    if (storedDataValid) {
                        dataSource = storedData;
                    }
                    else {
                        dataSource = pagedDataSource.GetEnumerator();
                    }

                    if (collection != null) {
                        int initialCapacity = pagedDataSource.Count;
                        keysArray.Capacity = initialCapacity;
                        itemsArray.Capacity = initialCapacity;
                    }
                }
            }

            int columnCount = 0;
            if (columnsArray != null)
                columnCount = columnsArray.Count;

            if (columnCount > 0) {
                DataGridColumn[] displayColumns = new DataGridColumn[columnCount];
                columnsArray.CopyTo(displayColumns, 0);

                Table table = new ChildTable(String.IsNullOrEmpty(ID) ? null : ClientID);
                Controls.Add(table);
                
                for (int c = 0; c < displayColumns.Length; c++) {
                    displayColumns[c].Initialize();
                }

                TableRowCollection rows = table.Rows;
                DataGridItem item;
                ListItemType itemType;
                int index = 0;
                int dataSetIndex = 0;

                string keyField = DataKeyField;
                bool storeKeys = (useDataSource && (keyField.Length != 0));
                bool createPager = pagedDataSource.IsPagingEnabled;
                int editItemIndex = EditItemIndex;
                int selectedItemIndex = SelectedIndex;

                if (pagedDataSource.IsPagingEnabled)
                    dataSetIndex = pagedDataSource.FirstIndexInPage;

                count = 0;

                if (createPager) {
                    // top pager
                    CreateItem(-1, -1, ListItemType.Pager, false, null, displayColumns, rows, pagedDataSource);
                }

                CreateItem(-1, -1, ListItemType.Header, useDataSource, null, displayColumns, rows, null);

                if (storedDataValid && (firstDataItem != null)) {
                    if (storeKeys) {
                        object keyValue = DataBinder.GetPropertyValue(firstDataItem, keyField);
                        keysArray.Add(keyValue);
                    }

                    itemType = ListItemType.Item;
                    if (index == editItemIndex)
                        itemType = ListItemType.EditItem;
                    else if (index == selectedItemIndex)
                        itemType = ListItemType.SelectedItem;

                    item = CreateItem(0, dataSetIndex, itemType, useDataSource, firstDataItem, displayColumns, rows, null);
                    itemsArray.Add(item);

                    count++;
                    index++;
                    dataSetIndex++;

                    storedDataValid = false;
                    firstDataItem = null;
                }

                while (dataSource.MoveNext()) {
                    object dataItem = dataSource.Current;

                    if (storeKeys) {
                        object keyValue = DataBinder.GetPropertyValue(dataItem, keyField);
                        keysArray.Add(keyValue);
                    }

                    itemType = ListItemType.Item;

                    if (index == editItemIndex)
                        itemType = ListItemType.EditItem;
                    else if (index == selectedItemIndex)
                        itemType = ListItemType.SelectedItem;
                    else if (index % 2 != 0) {
                        itemType = ListItemType.AlternatingItem;
                    }

                    item = CreateItem(index, dataSetIndex, itemType, useDataSource, dataItem, displayColumns, rows, null);
                    itemsArray.Add(item);

                    count++;
                    dataSetIndex++;
                    index++;
                }

                CreateItem(-1, -1, ListItemType.Footer, useDataSource, null, displayColumns, rows, null);

                if (createPager) {
                    // bottom pager
                    CreateItem(-1, -1, ListItemType.Pager, false, null, displayColumns, rows, pagedDataSource);
                }
            }

            if (useDataSource) {
                // save the number of items and pages contained in the DataGrid for use in round-trips
                if (dataSource != null) {
                    ViewState[BaseDataList.ItemCountViewStateKey] = count;
                    if (pagedDataSource.IsPagingEnabled) {
                        ViewState["PageCount"] = pagedDataSource.PageCount;
                        ViewState[DataSourceItemCountViewStateKey] = pagedDataSource.DataSourceCount;
                    }
                    else {
                        ViewState["PageCount"] = 1;
                        ViewState[DataSourceItemCountViewStateKey] = count;
                    }
                }
                else {
                    ViewState[BaseDataList.ItemCountViewStateKey] = -1;
                    ViewState[DataSourceItemCountViewStateKey] = -1;
                    ViewState["PageCount"] = 0;
                }
            }

            pagedDataSource = null;
        }
 protected override void CreateControlHierarchy(bool useDataSource)
 {
     this.pagedDataSource = this.CreatePagedDataSource();
     IEnumerator storedData = null;
     int dataItemCount = -1;
     int num2 = -1;
     ArrayList dataKeysArray = base.DataKeysArray;
     ArrayList list2 = null;
     if (this.itemsArray != null)
     {
         this.itemsArray.Clear();
     }
     else
     {
         this.itemsArray = new ArrayList();
     }
     this.itemsCollection = null;
     if (!useDataSource)
     {
         dataItemCount = (int) this.ViewState["_!ItemCount"];
         num2 = (int) this.ViewState["_!DataSourceItemCount"];
         if (dataItemCount != -1)
         {
             if (this.pagedDataSource.IsCustomPagingEnabled)
             {
                 this.pagedDataSource.DataSource = new DummyDataSource(dataItemCount);
             }
             else
             {
                 this.pagedDataSource.DataSource = new DummyDataSource(num2);
             }
             storedData = this.pagedDataSource.GetEnumerator();
             list2 = this.CreateColumnSet(null, false);
             this.itemsArray.Capacity = dataItemCount;
         }
     }
     else
     {
         dataKeysArray.Clear();
         IEnumerable data = this.GetData();
         if (data != null)
         {
             ICollection is2 = data as ICollection;
             if (((is2 == null) && this.pagedDataSource.IsPagingEnabled) && !this.pagedDataSource.IsCustomPagingEnabled)
             {
                 throw new HttpException(System.Web.SR.GetString("DataGrid_Missing_VirtualItemCount", new object[] { this.ID }));
             }
             this.pagedDataSource.DataSource = data;
             if (this.pagedDataSource.IsPagingEnabled && ((this.pagedDataSource.CurrentPageIndex < 0) || (this.pagedDataSource.CurrentPageIndex >= this.pagedDataSource.PageCount)))
             {
                 throw new HttpException(System.Web.SR.GetString("Invalid_CurrentPageIndex"));
             }
             list2 = this.CreateColumnSet(this.pagedDataSource, useDataSource);
             if (this.storedDataValid)
             {
                 storedData = this.storedData;
             }
             else
             {
                 storedData = this.pagedDataSource.GetEnumerator();
             }
             if (is2 != null)
             {
                 int count = this.pagedDataSource.Count;
                 dataKeysArray.Capacity = count;
                 this.itemsArray.Capacity = count;
             }
         }
     }
     int num4 = 0;
     if (list2 != null)
     {
         num4 = list2.Count;
     }
     if (num4 > 0)
     {
         DataGridItem item;
         ListItemType editItem;
         DataGridColumn[] array = new DataGridColumn[num4];
         list2.CopyTo(array, 0);
         Table child = new ChildTable(string.IsNullOrEmpty(this.ID) ? null : this.ClientID);
         this.Controls.Add(child);
         for (int i = 0; i < array.Length; i++)
         {
             array[i].Initialize();
         }
         TableRowCollection rows = child.Rows;
         int itemIndex = 0;
         int dataSourceIndex = 0;
         string dataKeyField = this.DataKeyField;
         bool flag = useDataSource && (dataKeyField.Length != 0);
         bool isPagingEnabled = this.pagedDataSource.IsPagingEnabled;
         int editItemIndex = this.EditItemIndex;
         int selectedIndex = this.SelectedIndex;
         if (this.pagedDataSource.IsPagingEnabled)
         {
             dataSourceIndex = this.pagedDataSource.FirstIndexInPage;
         }
         dataItemCount = 0;
         if (isPagingEnabled)
         {
             this.CreateItem(-1, -1, ListItemType.Pager, false, null, array, rows, this.pagedDataSource);
         }
         this.CreateItem(-1, -1, ListItemType.Header, useDataSource, null, array, rows, null);
         if (this.storedDataValid && (this.firstDataItem != null))
         {
             if (flag)
             {
                 object propertyValue = DataBinder.GetPropertyValue(this.firstDataItem, dataKeyField);
                 dataKeysArray.Add(propertyValue);
             }
             editItem = ListItemType.Item;
             if (itemIndex == editItemIndex)
             {
                 editItem = ListItemType.EditItem;
             }
             else if (itemIndex == selectedIndex)
             {
                 editItem = ListItemType.SelectedItem;
             }
             item = this.CreateItem(0, dataSourceIndex, editItem, useDataSource, this.firstDataItem, array, rows, null);
             this.itemsArray.Add(item);
             dataItemCount++;
             itemIndex++;
             dataSourceIndex++;
             this.storedDataValid = false;
             this.firstDataItem = null;
         }
         while (storedData.MoveNext())
         {
             object current = storedData.Current;
             if (flag)
             {
                 object obj4 = DataBinder.GetPropertyValue(current, dataKeyField);
                 dataKeysArray.Add(obj4);
             }
             editItem = ListItemType.Item;
             if (itemIndex == editItemIndex)
             {
                 editItem = ListItemType.EditItem;
             }
             else if (itemIndex == selectedIndex)
             {
                 editItem = ListItemType.SelectedItem;
             }
             else if ((itemIndex % 2) != 0)
             {
                 editItem = ListItemType.AlternatingItem;
             }
             item = this.CreateItem(itemIndex, dataSourceIndex, editItem, useDataSource, current, array, rows, null);
             this.itemsArray.Add(item);
             dataItemCount++;
             dataSourceIndex++;
             itemIndex++;
         }
         this.CreateItem(-1, -1, ListItemType.Footer, useDataSource, null, array, rows, null);
         if (isPagingEnabled)
         {
             this.CreateItem(-1, -1, ListItemType.Pager, false, null, array, rows, this.pagedDataSource);
         }
     }
     if (useDataSource)
     {
         if (storedData != null)
         {
             this.ViewState["_!ItemCount"] = dataItemCount;
             if (this.pagedDataSource.IsPagingEnabled)
             {
                 this.ViewState["PageCount"] = this.pagedDataSource.PageCount;
                 this.ViewState["_!DataSourceItemCount"] = this.pagedDataSource.DataSourceCount;
             }
             else
             {
                 this.ViewState["PageCount"] = 1;
                 this.ViewState["_!DataSourceItemCount"] = dataItemCount;
             }
         }
         else
         {
             this.ViewState["_!ItemCount"] = -1;
             this.ViewState["_!DataSourceItemCount"] = -1;
             this.ViewState["PageCount"] = 0;
         }
     }
     this.pagedDataSource = null;
 }