System.Web.UI.WebControls.DataGrid.CreateControlHierarchy C# (CSharp) Method

CreateControlHierarchy() protected method

protected CreateControlHierarchy ( bool useDataSource ) : void
useDataSource bool
return void
		protected override void CreateControlHierarchy (bool useDataSource)
		{
			Controls.Clear ();
			RenderTable.Controls.Clear ();
			Controls.Add (RenderTable);
			
			IEnumerable data_source;
			ArrayList keys = null;
			if (useDataSource) {
#if NET_2_0
				if (IsBoundUsingDataSourceID)
					data_source = GetData ();
				else
#endif
				data_source = DataSourceResolver.ResolveDataSource (DataSource, DataMember);
				if (data_source == null) {
					Controls.Clear ();
					return;
				}
				
				keys = DataKeysArray;
				keys.Clear ();
			} else {
				int nitems = ViewState.GetInt ("Items", 0);
				data_source = new NCollection (nitems);
			}

			paged_data_source = new PagedDataSource ();
			PagedDataSource pds = paged_data_source;
			pds.AllowPaging = AllowPaging;
			pds.AllowCustomPaging = AllowCustomPaging;
			pds.DataSource = data_source;
			pds.CurrentPageIndex = CurrentPageIndex;
			pds.PageSize = PageSize;
			pds.VirtualCount = VirtualItemCount;

			if ((pds.IsPagingEnabled) && (pds.PageCount < pds.CurrentPageIndex)) {
				Controls.Clear ();
				throw new HttpException ("Invalid DataGrid PageIndex");
			}
			
			ArrayList cList = CreateColumnSet (paged_data_source, useDataSource);
			if (cList.Count == 0) {
				Controls.Clear ();
				return;
			}
			
			Page page = this.Page;
			if (page != null)
				page.RequiresPostBackScript ();
			
			render_columns = new DataGridColumn [cList.Count];
			for (int c = 0; c < cList.Count; c++) {
				DataGridColumn col = (DataGridColumn) cList [c];
				col.Set_Owner (this);
				col.Initialize ();
				render_columns [c] = col;
			}

			if (pds.IsPagingEnabled)
				CreateItem (-1, -1, ListItemType.Pager, false, null, pds);

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

			// No indexer on PagedDataSource so we have to do
			// this silly foreach and index++
			if (items_list == null)
				items_list = new ArrayList ();
			else
				items_list.Clear();

			bool skip_first = false;
			IEnumerator enumerator = null;
			if (data_enumerator != null) {
				// replaced when creating bound columns
				enumerator = data_enumerator;
				skip_first = true;
			} else if (pds.DataSource != null) {
				enumerator = pds.GetEnumerator ();
			} else {
				enumerator = null;
			}

			int index = 0;
			bool first = true;
			string key = null;
			int dataset_index = pds.FirstIndexInPage;
			int selected_index = SelectedIndex;
			int edit_item_index = EditItemIndex;
			while (enumerator != null && (skip_first || enumerator.MoveNext ())) {
				// MS does not render <table blah></table> on empty datasource.
				if (first) {
					first = false;
					key = DataKeyField;
					skip_first = false;
				}
				object data = enumerator.Current;
				// This will throw if the DataKeyField is not there. As on MS, this
				// will not be hit on an empty datasource.
				// The values stored here can be used in events so that you can
				// get, for example, the row that was clicked
				// (data.Rows.Find (ItemsGrid.DataKeys [e.Item.ItemIndex]))
				// BaseDataList will keep the array across postbacks.
				if (useDataSource && key != "")
					keys.Add (DataBinder.GetPropertyValue (data, key));

				ListItemType type = ListItemType.Item;
				if (index == edit_item_index) 
					type = ListItemType.EditItem;
				else if (index == selected_index) 
					type = ListItemType.SelectedItem;
				else if (index % 2 != 0) 
					type = ListItemType.AlternatingItem;

				items_list.Add (CreateItem (index, dataset_index, type, useDataSource, data, pds));
				index++;
				dataset_index++;
			}

			CreateItem (-1, -1, ListItemType.Footer, useDataSource, null, paged_data_source);
			if (pds.IsPagingEnabled) {
				CreateItem (-1, -1, ListItemType.Pager, false, null, paged_data_source);
				if (useDataSource)
					ViewState ["Items"] = pds.IsCustomPagingEnabled ? index : pds.DataSourceCount;
			} else if (useDataSource) {
				ViewState ["Items"] = index;
			}
		}