/// <devdoc> /// </devdoc> private ArrayList CreateAutoGeneratedColumns(PagedDataSource dataSource) { if (dataSource == null) { // note that we're not throwing an exception in this case, and the calling // code should be able to handle a null arraylist being returned return null; } ArrayList generatedColumns = new ArrayList(); PropertyDescriptorCollection propDescs = null; bool throwException = true; // try ITypedList first // A PagedDataSource implements this, but returns null, if the underlying data source // does not implement it. propDescs = ((ITypedList)dataSource).GetItemProperties(new PropertyDescriptor[0]); if (propDescs == null) { Type sampleItemType = null; object sampleItem = null; IEnumerable realDataSource = dataSource.DataSource; Debug.Assert(realDataSource != null, "Must have a real data source when calling CreateAutoGeneratedColumns"); Type dataSourceType = realDataSource.GetType(); // try for a typed Item property, which should be present on strongly typed collections PropertyInfo itemProp = dataSourceType.GetProperty("Item", BindingFlags.Public | BindingFlags.Instance, null, null, new Type[] { typeof(int) }, null); if (itemProp != null) { sampleItemType = itemProp.PropertyType; } if ((sampleItemType == null) || (sampleItemType == typeof(object))) { // last resort... try to get ahold of the first item by beginning the // enumeration IEnumerator e = dataSource.GetEnumerator(); if (e.MoveNext()) { sampleItem = e.Current; } else { // we don't want to throw an exception if we're bound to an IEnumerable // data source with no records... we'll simply bail and not show any data throwException = false; } if (sampleItem != null) { sampleItemType = sampleItem.GetType(); } // We must store the enumerator regardless of whether we got back an item from it // because we cannot start the enumeration again, in the case of a DataReader. // Code in CreateControlHierarchy must deal appropriately for the case where // there is a stored enumerator, but a null object as the first item. StoreEnumerator(e, sampleItem); } if ((sampleItem != null) && (sampleItem is ICustomTypeDescriptor)) { // Get the custom properties of the object propDescs = TypeDescriptor.GetProperties(sampleItem); } else if (sampleItemType != null) { // directly bindable types: strings, ints etc. get treated special, since we // don't care about their properties, but rather we care about them directly if (BaseDataList.IsBindableType(sampleItemType)) { BoundColumn column = new BoundColumn(); ((IStateManager)column).TrackViewState(); column.HeaderText = "Item"; column.DataField = BoundColumn.thisExpr; column.SortExpression = "Item"; column.SetOwner(this); generatedColumns.Add(column); } else { // complex type... we get its properties propDescs = TypeDescriptor.GetProperties(sampleItemType); } } } if ((propDescs != null) && (propDescs.Count != 0)) { foreach (PropertyDescriptor pd in propDescs) { Type propType = pd.PropertyType; if (BaseDataList.IsBindableType(propType)) { BoundColumn column = new BoundColumn(); ((IStateManager)column).TrackViewState(); column.HeaderText = pd.Name; column.DataField = pd.Name; column.SortExpression = pd.Name; column.ReadOnly = pd.IsReadOnly; column.SetOwner(this); generatedColumns.Add(column); } } } if ((generatedColumns.Count == 0) && throwException) { // this handles the case where we got back something that either had no // properties, or all properties were not bindable. throw new HttpException(SR.GetString(SR.DataGrid_NoAutoGenColumns, ID)); } return generatedColumns; }
/// <internalonly/> /// <devdoc> /// <para>Loads a saved state of the <see cref='System.Web.UI.WebControls.DataGrid'/>.</para> /// </devdoc> protected override void LoadViewState(object savedState) { if (savedState != null) { object[] myState = (object[])savedState; if (myState[0] != null) base.LoadViewState(myState[0]); if (myState[1] != null) ((IStateManager)Columns).LoadViewState(myState[1]); if (myState[2] != null) ((IStateManager)PagerStyle).LoadViewState(myState[2]); if (myState[3] != null) ((IStateManager)HeaderStyle).LoadViewState(myState[3]); if (myState[4] != null) ((IStateManager)FooterStyle).LoadViewState(myState[4]); if (myState[5] != null) ((IStateManager)ItemStyle).LoadViewState(myState[5]); if (myState[6] != null) ((IStateManager)AlternatingItemStyle).LoadViewState(myState[6]); if (myState[7] != null) ((IStateManager)SelectedItemStyle).LoadViewState(myState[7]); if (myState[8] != null) ((IStateManager)EditItemStyle).LoadViewState(myState[8]); if (myState[9] != null) ((IStateManager)ControlStyle).LoadViewState(myState[9]); if (myState[10] != null) { object[] autoGenColumnState = (object[])myState[10]; int columnCount = autoGenColumnState.Length; if (columnCount != 0) autoGenColumnsArray = new ArrayList(); else autoGenColumnsArray = null; for (int i = 0; i < columnCount; i++) { BoundColumn column = new BoundColumn(); ((IStateManager)column).TrackViewState(); ((IStateManager)column).LoadViewState(autoGenColumnState[i]); column.SetOwner(this); autoGenColumnsArray.Add(column); } } } }
protected override void LoadViewState(object savedState) { if (savedState == null) return; object [] states = (object []) savedState; base.LoadViewState (states[0]); if(columns != null) ((IStateManager)columns).LoadViewState(states[1]); if(pagerStyle != null) pagerStyle.LoadViewState(states[2]); if(headerStyle != null) headerStyle.LoadViewState(states[3]); if(footerStyle != null) footerStyle.LoadViewState(states[4]); if(itemStyle != null) itemStyle.LoadViewState(states[5]); if(alternatingItemStyle != null) alternatingItemStyle.LoadViewState(states[6]); if(selectedItemStyle != null) selectedItemStyle.LoadViewState(states[7]); if(editItemStyle != null) editItemStyle.LoadViewState(states[8]); if (states [9] != null) { object[] array = ((object[]) states [9]); if (array.Length != 0) this.autoGenColsArrayList = new ArrayList (); else this.autoGenColsArrayList = null; for (int i = 0; i < array.Length; i++) { BoundColumn column1 = new BoundColumn (); ((IStateManager)column1).TrackViewState (); ((IStateManager)column1).LoadViewState (array [i]); column1.SetOwner (this); this.autoGenColsArrayList.Add (column1); } } }
/// <summary> /// Generates the columns when AutoGenerateColumns is true. /// This method is called by CreateColumnSet when dataSource /// is to be used and columns need to be generated automatically. /// </summary> private ArrayList AutoCreateColumns (PagedDataSource source) { if (source == null) return null; ArrayList retVal = null; PropertyDescriptorCollection props = source.GetItemProperties (new PropertyDescriptor [0]); bool empty_enumerator = false; Type prop_type; BoundColumn col; if (props == null) { object fitem = null; prop_type = null; PropertyInfo prop_item = source.DataSource.GetType ().GetProperty ("Item", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public, null, null, new Type[] { typeof(int) }, null); if (prop_item != null) prop_type = prop_item.PropertyType; if (prop_type == null || prop_type == typeof (object)) { IEnumerator en = source.GetEnumerator(); if (en.MoveNext ()) { fitem = en.Current; if (fitem != null) prop_type = fitem.GetType (); } else { empty_enumerator = true; } StoreEnumerator (en, fitem); } if (fitem is ICustomTypeDescriptor) { props = TypeDescriptor.GetProperties (fitem); } else if (prop_type != null) { if (IsBindableType (prop_type)) { col = new BoundColumn (); ((IStateManager) col).TrackViewState (); col.HeaderText = "Item"; col.SortExpression = "Item"; col.DataField = BoundColumn.thisExpr; col.SetOwner (this); if (retVal == null) retVal = new ArrayList (); retVal.Add (col); } else { props = TypeDescriptor.GetProperties (prop_type); } } } if (props != null && props.Count > 0) { try { foreach (PropertyDescriptor current in props) { if (!IsBindableType (current.PropertyType)) continue; col = new BoundColumn (); ((IStateManager) col).TrackViewState (); string name = current.Name; col.HeaderText = name; col.SortExpression = name; col.DataField = name; col.ReadOnly = current.IsReadOnly; col.SetOwner (this); if (retVal == null) retVal = new ArrayList (); retVal.Add (col); } } finally { if (props is IDisposable) ((IDisposable) props).Dispose (); } } if (retVal != null && retVal.Count > 0) return retVal; if (empty_enumerator) return null; throw new HttpException (HttpRuntime.FormatResourceString ("DataGrid_NoAutoGenColumns", ID)); }
protected override void LoadViewState(object savedState) { if (savedState != null) { object[] objArray = (object[]) savedState; if (objArray[0] != null) { base.LoadViewState(objArray[0]); } if (objArray[1] != null) { ((IStateManager) this.Columns).LoadViewState(objArray[1]); } if (objArray[2] != null) { ((IStateManager) this.PagerStyle).LoadViewState(objArray[2]); } if (objArray[3] != null) { ((IStateManager) this.HeaderStyle).LoadViewState(objArray[3]); } if (objArray[4] != null) { ((IStateManager) this.FooterStyle).LoadViewState(objArray[4]); } if (objArray[5] != null) { ((IStateManager) this.ItemStyle).LoadViewState(objArray[5]); } if (objArray[6] != null) { ((IStateManager) this.AlternatingItemStyle).LoadViewState(objArray[6]); } if (objArray[7] != null) { ((IStateManager) this.SelectedItemStyle).LoadViewState(objArray[7]); } if (objArray[8] != null) { ((IStateManager) this.EditItemStyle).LoadViewState(objArray[8]); } if (objArray[9] != null) { ((IStateManager) base.ControlStyle).LoadViewState(objArray[9]); } if (objArray[10] != null) { object[] objArray2 = (object[]) objArray[10]; int length = objArray2.Length; if (length != 0) { this.autoGenColumnsArray = new ArrayList(); } else { this.autoGenColumnsArray = null; } for (int i = 0; i < length; i++) { BoundColumn column = new BoundColumn(); ((IStateManager) column).TrackViewState(); ((IStateManager) column).LoadViewState(objArray2[i]); column.SetOwner(this); this.autoGenColumnsArray.Add(column); } } } }
private ArrayList CreateAutoGeneratedColumns(PagedDataSource dataSource) { if (dataSource == null) { return null; } ArrayList list = new ArrayList(); PropertyDescriptorCollection itemProperties = null; bool flag = true; itemProperties = dataSource.GetItemProperties(new PropertyDescriptor[0]); if (itemProperties == null) { Type propertyType = null; object firstDataItem = null; PropertyInfo info = dataSource.DataSource.GetType().GetProperty("Item", BindingFlags.Public | BindingFlags.Instance, null, null, new Type[] { typeof(int) }, null); if (info != null) { propertyType = info.PropertyType; } if ((propertyType == null) || (propertyType == typeof(object))) { IEnumerator enumerator = dataSource.GetEnumerator(); if (enumerator.MoveNext()) { firstDataItem = enumerator.Current; } else { flag = false; } if (firstDataItem != null) { propertyType = firstDataItem.GetType(); } this.StoreEnumerator(enumerator, firstDataItem); } if ((firstDataItem != null) && (firstDataItem is ICustomTypeDescriptor)) { itemProperties = TypeDescriptor.GetProperties(firstDataItem); } else if (propertyType != null) { if (BaseDataList.IsBindableType(propertyType)) { BoundColumn column = new BoundColumn(); ((IStateManager) column).TrackViewState(); column.HeaderText = "Item"; column.DataField = BoundColumn.thisExpr; column.SortExpression = "Item"; column.SetOwner(this); list.Add(column); } else { itemProperties = TypeDescriptor.GetProperties(propertyType); } } } if ((itemProperties != null) && (itemProperties.Count != 0)) { foreach (PropertyDescriptor descriptor in itemProperties) { if (BaseDataList.IsBindableType(descriptor.PropertyType)) { BoundColumn column2 = new BoundColumn(); ((IStateManager) column2).TrackViewState(); column2.HeaderText = descriptor.Name; column2.DataField = descriptor.Name; column2.SortExpression = descriptor.Name; column2.ReadOnly = descriptor.IsReadOnly; column2.SetOwner(this); list.Add(column2); } } } if ((list.Count == 0) && flag) { throw new HttpException(System.Web.SR.GetString("DataGrid_NoAutoGenColumns", new object[] { this.ID })); } return list; }