protected override int CreateChildControls (IEnumerable data, bool dataBinding) { PagedDataSource dataSource = new PagedDataSource (); dataSource.DataSource = CurrentMode != FormViewMode.Insert ? data : null; dataSource.AllowPaging = AllowPaging; dataSource.PageSize = 1; dataSource.CurrentPageIndex = PageIndex; if (dataBinding && CurrentMode != FormViewMode.Insert) { DataSourceView view = GetData (); if (view != null && view.CanPage) { dataSource.AllowServerPaging = true; if (SelectArguments.RetrieveTotalRowCount) dataSource.VirtualCount = SelectArguments.TotalRowCount; } } PagerSettings pagerSettings = PagerSettings; bool showPager = AllowPaging && pagerSettings.Visible && (dataSource.PageCount > 1); Controls.Clear (); table = CreateTable (); Controls.Add (table); headerRow = null; footerRow = null; topPagerRow = null; bottomPagerRow = null; // Gets the current data item if (AllowPaging) { PageCount = dataSource.DataSourceCount; if (PageIndex >= PageCount && PageCount > 0) pageIndex = dataSource.CurrentPageIndex = PageCount - 1; if (dataSource.DataSource != null) { IEnumerator e = dataSource.GetEnumerator (); if (e.MoveNext ()) dataItem = e.Current; } } else { int page = 0; object lastItem = null; if (dataSource.DataSource != null) { IEnumerator e = dataSource.GetEnumerator (); for (; e.MoveNext (); page++) { lastItem = e.Current; if (page == PageIndex) dataItem = e.Current; } } PageCount = page; if (PageIndex >= PageCount && PageCount > 0) { pageIndex = PageCount - 1; dataItem = lastItem; } } // Main table creation bool emptyRow = PageCount == 0 && CurrentMode != FormViewMode.Insert; if (!emptyRow) { headerRow = CreateRow (-1, DataControlRowType.Header, DataControlRowState.Normal); InitializeRow (headerRow); table.Rows.Add (headerRow); } if (showPager && pagerSettings.Position == PagerPosition.Top || pagerSettings.Position == PagerPosition.TopAndBottom) { topPagerRow = CreateRow (-1, DataControlRowType.Pager, DataControlRowState.Normal); InitializePager (topPagerRow, dataSource); table.Rows.Add (topPagerRow); } if (PageCount > 0) { DataControlRowState rstate = GetRowState (); itemRow = CreateRow (0, DataControlRowType.DataRow, rstate); InitializeRow (itemRow); table.Rows.Add (itemRow); } else { switch (CurrentMode) { case FormViewMode.Edit: itemRow = CreateRow (-1, DataControlRowType.EmptyDataRow, DataControlRowState.Edit); break; case FormViewMode.Insert: itemRow = CreateRow (-1, DataControlRowType.DataRow, DataControlRowState.Insert); break; default: itemRow = CreateRow (-1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal); break; } InitializeRow (itemRow); table.Rows.Add (itemRow); } if (!emptyRow) { footerRow = CreateRow (-1, DataControlRowType.Footer, DataControlRowState.Normal); InitializeRow (footerRow); table.Rows.Add (footerRow); } if (showPager && pagerSettings.Position == PagerPosition.Bottom || pagerSettings.Position == PagerPosition.TopAndBottom) { bottomPagerRow = CreateRow (0, DataControlRowType.Pager, DataControlRowState.Normal); InitializePager (bottomPagerRow, dataSource); table.Rows.Add (bottomPagerRow); } OnItemCreated (EventArgs.Empty); if (dataBinding) DataBind (false); return PageCount; }
protected override void CreateControlHierarchy(bool useDataSource) { IEnumerator pageSourceEnumerator; int itemCount; ArrayList dataKeys; ArrayList columns; IEnumerable resolvedDS; ICollection collResolvedDS; int pageDSCount; int colCount; DataGridColumn[] cols; Table deployTable; TableRowCollection deployRows; ListItemType deployType; int indexCounter; string dkField; bool dsUse; bool pgEnabled; int editIndex; int selIndex; pagedDataSource = CreatePagedDataSource(); pageSourceEnumerator = null; itemCount = -1; dataKeys = DataKeysArray; columns = null; if(itemsArrayList != null) { itemsArrayList.Clear(); } else { itemsArrayList = new ArrayList(); } if(!useDataSource) { itemCount = (int) ViewState["_!ItemCount"]; pageDSCount = (int) ViewState["_!DataSource_ItemCount"]; if(itemCount != -1) { if(pagedDataSource.IsCustomPagingEnabled) { pagedDataSource.DataSource = new DataSourceInternal(itemCount); } else { pagedDataSource.DataSource = new DataSourceInternal(pageDSCount); } pageSourceEnumerator = pagedDataSource.GetEnumerator(); columns = CreateColumnSet(null, false); itemsArrayList.Capacity = itemCount; } } else { dataKeys.Clear(); resolvedDS = GetResolvedDataSource (); if(resolvedDS != null) { collResolvedDS = resolvedDS as ICollection; if(pagedDataSource.IsPagingEnabled && !pagedDataSource.IsCustomPagingEnabled && collResolvedDS == null) { throw new HttpException(HttpRuntime.FormatResourceString("DataGrid_Missing_VirtualItemCount", ID)); } pagedDataSource.DataSource = resolvedDS; if(pagedDataSource.IsPagingEnabled && (pagedDataSource.CurrentPageIndex < 0 || pagedDataSource.PageCount < pagedDataSource.CurrentPageIndex)) { throw new HttpException(HttpRuntime.FormatResourceString("DataGrid_Invalid_Current_PageIndex", ID)); } columns = CreateColumnSet(pagedDataSource, useDataSource); if(storedDataValid) { pageSourceEnumerator = storedData; } else { pageSourceEnumerator = pagedDataSource.GetEnumerator(); } if(collResolvedDS != null) { pageDSCount = pagedDataSource.Count; dataKeys.Capacity = pageDSCount; itemsArrayList.Capacity = pageDSCount; } } } colCount = 0; if(columns != null) colCount = columns.Count; int currentSourceIndex; if(colCount > 0) { cols = (DataGridColumn []) columns.ToArray (typeof (DataGridColumn)); foreach(DataGridColumn current in cols) { current.Initialize(); } deployTable = new DataGridTableInternal(); Controls.Add(deployTable); deployRows = deployTable.Rows; indexCounter = 0; currentSourceIndex = 0; dkField = DataKeyField; dsUse = (useDataSource) ? (dkField.Length > 0) : false; pgEnabled = pagedDataSource.IsPagingEnabled; editIndex = EditItemIndex; selIndex = SelectedIndex; if(pgEnabled) { currentSourceIndex = pagedDataSource.FirstIndexInPage; CreateItem(-1, -1, ListItemType.Pager, false, null, cols, deployRows, pagedDataSource); } itemCount = 0; CreateItem(-1, -1, ListItemType.Header, useDataSource, null, cols, deployRows, null); if(storedDataValid && storedDataFirst != null) { if(dsUse) { dataKeys.Add(DataBinder.GetPropertyValue(storedDataFirst, dkField)); } if (indexCounter == editIndex) { deployType = ListItemType.EditItem; } else if (indexCounter == selIndex) { deployType = ListItemType.SelectedItem; } else { deployType = ListItemType.Item; } itemsArrayList.Add(CreateItem(0, currentSourceIndex, deployType, useDataSource, storedDataFirst, cols, deployRows, null)); itemCount++; indexCounter++; currentSourceIndex++; storedDataValid = false; storedDataFirst = null; } while(pageSourceEnumerator.MoveNext()) { object current = pageSourceEnumerator.Current; if(dsUse) { dataKeys.Add(DataBinder.GetPropertyValue(current, dkField)); } if (indexCounter == editIndex) { deployType = ListItemType.EditItem; } else if (indexCounter == selIndex) { deployType = ListItemType.SelectedItem; } else if ((indexCounter % 2) == 1) { deployType = ListItemType.AlternatingItem; } else { deployType = ListItemType.Item; } itemsArrayList.Add(CreateItem(indexCounter, currentSourceIndex, deployType, useDataSource, current, cols, deployRows, null)); itemCount++; indexCounter++; currentSourceIndex++; } CreateItem(-1, -1, ListItemType.Footer, useDataSource, null, cols, deployRows, null); if(pgEnabled) { CreateItem(-1, -1, ListItemType.Pager, false, null, cols, deployRows, pagedDataSource); } } if(useDataSource) { if(pageSourceEnumerator != null) { ViewState["_!ItemCount"] = itemCount; if(pagedDataSource.IsPagingEnabled) { ViewState["PageCount"] = pagedDataSource.PageCount; ViewState["_!DataSource_ItemCount"] = pagedDataSource.DataSourceCount; } else { ViewState["PageCount"] = 1; ViewState["_!DataSource_ItemCount"] = itemCount; } } else { ViewState["_!ItemCount"] = -1; ViewState["_!DataSource_ItemCount"] = -1; ViewState["PageCount"] = 0; } } pagedDataSource = null; }
void AddColumnsFromSource (PagedDataSource data_source) { PropertyDescriptorCollection props = null; Type ptype = null; bool no_items = false; // Use plain reflection for the Item property. // If we use TypeDescriptor, props will hold // all of the Type properties, which will be listed as columns Type ds_type = data_source.GetType (); PropertyInfo pinfo = ds_type.GetProperty ("Item", item_args); if (pinfo == null) { IEnumerator items = (data_source.DataSource != null) ? data_source.GetEnumerator () : null; if (items != null && items.MoveNext ()) { object data = items.Current; if ((data is ICustomTypeDescriptor) || (!IsBindableType(data.GetType()))) props = TypeDescriptor.GetProperties (data); else if (data != null) ptype = data.GetType (); data_enumerator = items; } else { no_items = true; } } else { ptype = pinfo.PropertyType; } if (ptype != null) { // Found the "Item" property AddPropertyToColumns (); } else if (props != null) { foreach (PropertyDescriptor pd in props) AddPropertyToColumns (pd, false); } else if (!no_items) { // This is not thrown for an empty ArrayList. string msg = String.Format ("DataGrid '{0}' cannot autogenerate " + "columns from the given datasource. {1}", ID, ptype); throw new HttpException (msg); } }
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; }
private ICollection CreateAutoGeneratedColumns(PagedDataSource dataSource) { if (dataSource == null) { return null; } ArrayList list = new ArrayList(); this._autoGenFieldProps = new ArrayList(); PropertyDescriptorCollection itemProperties = null; bool flag = true; itemProperties = dataSource.GetItemProperties(new PropertyDescriptor[0]); if (itemProperties == null) { Type propertyType = null; object firstDataRow = 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()) { firstDataRow = enumerator.Current; } else { flag = false; } if (firstDataRow != null) { propertyType = firstDataRow.GetType(); } this.StoreEnumerator(enumerator, firstDataRow); } if ((firstDataRow != null) && (firstDataRow is ICustomTypeDescriptor)) { itemProperties = TypeDescriptor.GetProperties(firstDataRow); } else if (propertyType != null) { if (this.IsBindableType(propertyType)) { AutoGeneratedFieldProperties fieldProperties = new AutoGeneratedFieldProperties(); ((IStateManager) fieldProperties).TrackViewState(); fieldProperties.Type = propertyType; fieldProperties.Name = "Item"; fieldProperties.DataField = BoundField.ThisExpression; AutoGeneratedField field = this.CreateAutoGeneratedColumn(fieldProperties); if (field != null) { list.Add(field); this._autoGenFieldProps.Add(fieldProperties); } } else { itemProperties = TypeDescriptor.GetProperties(propertyType); } } } else if (itemProperties.Count == 0) { flag = false; } if ((itemProperties != null) && (itemProperties.Count != 0)) { string[] dataKeyNames = this.DataKeyNames; int length = dataKeyNames.Length; string[] strArray2 = new string[length]; for (int i = 0; i < length; i++) { strArray2[i] = dataKeyNames[i].ToLowerInvariant(); } foreach (PropertyDescriptor descriptor in itemProperties) { Type type = descriptor.PropertyType; if (this.IsBindableType(type)) { string name = descriptor.Name; bool flag2 = strArray2.Contains(name.ToLowerInvariant()); AutoGeneratedFieldProperties properties2 = new AutoGeneratedFieldProperties(); ((IStateManager) properties2).TrackViewState(); properties2.Name = name; properties2.IsReadOnly = flag2; properties2.Type = type; properties2.DataField = name; AutoGeneratedField field2 = this.CreateAutoGeneratedColumn(properties2); if (field2 != null) { list.Add(field2); this._autoGenFieldProps.Add(properties2); } } } } if ((list.Count == 0) && flag) { throw new HttpException(System.Web.SR.GetString("GridView_NoAutoGenFields", new object[] { this.ID })); } return list; }
public override List <AutoGeneratedField> CreateAutoGeneratedFields(object dataObject, Control control) { if (!(control is GridView)) { throw new ArgumentException(SR.GetString(SR.InvalidDefaultAutoFieldGenerator, GetType().FullName, typeof(GridView).FullName)); } Debug.Assert(dataObject == null || dataObject is PagedDataSource); PagedDataSource dataSource = dataObject as PagedDataSource; GridView gridView = control as GridView; 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); } List <AutoGeneratedField> generatedFields = new List <AutoGeneratedField>(); 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 Row 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 CreateChildControls must deal appropriately for the case where // there is a stored enumerator, but a null object as the first item. gridView.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 specially, since we // don't care about their properties, but rather we care about them directly if (ShouldGenerateField(sampleItemType, gridView)) { AutoGeneratedFieldProperties fieldProps = new AutoGeneratedFieldProperties(); ((IStateManager)fieldProps).TrackViewState(); fieldProps.Type = sampleItemType; fieldProps.Name = "Item"; fieldProps.DataField = AutoGeneratedField.ThisExpression; AutoGeneratedField field = CreateAutoGeneratedFieldFromFieldProperties(fieldProps); if (field != null) { generatedFields.Add(field); AutoGeneratedFieldProperties.Add(fieldProps); } } else { // complex type... we get its properties propDescs = TypeDescriptor.GetProperties(sampleItemType); } } } else { if (propDescs.Count == 0) { // we don't want to throw an exception if we're bound to an ITypedList // data source with no records... we'll simply bail and not show any data throwException = false; } } if ((propDescs != null) && (propDescs.Count != 0)) { string[] dataKeyNames = gridView.DataKeyNames; int keyNamesLength = dataKeyNames.Length; string[] dataKeyNamesCaseInsensitive = new string[keyNamesLength]; for (int i = 0; i < keyNamesLength; i++) { dataKeyNamesCaseInsensitive[i] = dataKeyNames[i].ToLowerInvariant(); } foreach (PropertyDescriptor pd in propDescs) { Type propertyType = pd.PropertyType; if (ShouldGenerateField(propertyType, gridView)) { string name = pd.Name; bool isKey = ((IList)dataKeyNamesCaseInsensitive).Contains(name.ToLowerInvariant()); AutoGeneratedFieldProperties fieldProps = new AutoGeneratedFieldProperties(); ((IStateManager)fieldProps).TrackViewState(); fieldProps.Name = name; fieldProps.IsReadOnly = isKey; fieldProps.Type = propertyType; fieldProps.DataField = name; AutoGeneratedField field = CreateAutoGeneratedFieldFromFieldProperties(fieldProps); if (field != null) { generatedFields.Add(field); AutoGeneratedFieldProperties.Add(fieldProps); } } } } if ((generatedFields.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 InvalidOperationException(SR.GetString(SR.GridView_NoAutoGenFields, gridView.ID)); } return(generatedFields); }
public void Paging4 () { PagedDataSource paged = new PagedDataSource (); paged.AllowPaging = true; paged.PageSize = 5; DataTable table = new DataTable (); FillTable (table, 100); paged.DataSource = new DataView (table); paged.CurrentPageIndex = 1; IEnumerator rator = paged.GetEnumerator (); Assert.IsTrue (rator.MoveNext (), "beginning-1"); DataRowView drv = (DataRowView) rator.Current; int one = Int32.Parse ((string) drv ["one"]); Assert.IsTrue (one == 0 || one == 1, "one-1"); int res = one + 2 * Int32.Parse ((string) drv ["two"]); Assert.AreEqual (5, res, "five"); }
/// <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; }
public void Paging1 () { PagedDataSource paged = new PagedDataSource (); paged.AllowPaging = true; paged.PageSize = 5; DataTable table = new DataTable (); FillTable (table, 100); paged.DataSource = new DataView (table); Assert.IsTrue (paged.IsFirstPage, "first-1"); Assert.IsFalse (paged.IsLastPage, "last-1"); paged.CurrentPageIndex = 100; // no problem setting this. Assert.AreEqual (100, paged.CurrentPageIndex, "current-1"); Assert.IsFalse (paged.IsFirstPage, "first-2"); Assert.IsFalse (paged.IsLastPage, "last-2"); IEnumerator rator = paged.GetEnumerator (); Assert.IsFalse (rator.MoveNext (), "beyondtheend-1"); }
public void Paging3 () { PagedDataSource paged = new PagedDataSource (); paged.AllowPaging = true; paged.PageSize = 5; DataTable table = new DataTable (); FillTable (table, 100); paged.DataSource = new DataView (table); paged.CurrentPageIndex = -7; Assert.AreEqual (-7, paged.CurrentPageIndex, "current"); Assert.IsFalse (paged.IsFirstPage, "first"); Assert.IsFalse (paged.IsLastPage, "last"); IEnumerator rator = paged.GetEnumerator (); Assert.AreEqual (-7, paged.CurrentPageIndex, "current-2"); Assert.IsTrue (rator.MoveNext (), "beyondtheend"); DataRowView drv = (DataRowView) rator.Current; // Throws (out of range) }
public void NullSource () { PagedDataSource ds = new PagedDataSource (); ds.DataSource = null; IEnumerator data = ds.GetEnumerator (); }
public void TestEnumerators_NoPaging () { PagedDataSource ds = new PagedDataSource (); ds.AllowPaging = false; // // Collection Enumerator // Queue q = new Queue (); for (int i = 0; i < 50; i++) q.Enqueue (i); ds.DataSource = q; EnumeratorTester_NoPaging (ds.GetEnumerator (), "collection"); // // List Enumerator // ArrayList l = new ArrayList (); for (int i = 0; i < 50; i++) l.Add (i); EnumeratorTester_NoPaging (ds.GetEnumerator (), "list"); }
public void TestEnumerators () { PagedDataSource ds = new PagedDataSource (); ds.AllowPaging = true; ds.PageSize = 10; ds.CurrentPageIndex = 2; // // Collection Enumerator // Queue q = new Queue (); for (int i = 0; i < 50; i++) q.Enqueue (i); ds.DataSource = q; EnumeratorTester (ds.GetEnumerator (), "collection"); // // List Enumerator // ArrayList l = new ArrayList (); for (int i = 0; i < 50; i++) l.Add (i); EnumeratorTester (ds.GetEnumerator (), "list"); }
/// <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)); }
public void VirtualPager2 () { PagedDataSource paged = new PagedDataSource (); paged.AllowPaging = true; paged.PageSize = 100; paged.VirtualCount = 50; paged.AllowCustomPaging = true; DataTable table = new DataTable (); FillTable (table, 100); paged.DataSource = new DataView (table); int count = 0; IEnumerator rator = paged.GetEnumerator (); while (rator.MoveNext ()) count++; Assert.AreEqual (100, count, "count"); Assert.AreEqual (true, paged.IsFirstPage, "first"); Assert.AreEqual (true, paged.IsLastPage, "last"); }
/// <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 int CreateChildControls (IEnumerable data, bool dataBinding) { PagedDataSource dataSource = new PagedDataSource (); dataSource.DataSource = CurrentMode != DetailsViewMode.Insert ? data : null; dataSource.AllowPaging = AllowPaging; dataSource.PageSize = 1; dataSource.CurrentPageIndex = PageIndex; if (dataBinding && CurrentMode != DetailsViewMode.Insert) { DataSourceView view = GetData (); if (view != null && view.CanPage) { dataSource.AllowServerPaging = true; if (SelectArguments.RetrieveTotalRowCount) dataSource.VirtualCount = SelectArguments.TotalRowCount; } } bool showPager = AllowPaging && (dataSource.PageCount > 1); Controls.Clear (); table = CreateTable (); Controls.Add (table); headerRow = null; footerRow = null; topPagerRow = null; bottomPagerRow = null; ArrayList list = new ArrayList (); // Gets the current data item if (AllowPaging) { PageCount = dataSource.DataSourceCount; if (PageIndex >= PageCount && PageCount > 0) { pageIndex = dataSource.CurrentPageIndex = PageCount - 1; } if (dataSource.DataSource != null) { IEnumerator e = dataSource.GetEnumerator (); if (e.MoveNext ()) dataItem = e.Current; } } else { int page = 0; object lastItem = null; if (dataSource.DataSource != null) { IEnumerator e = dataSource.GetEnumerator (); for (; e.MoveNext (); page++) { lastItem = e.Current; if (page == PageIndex) dataItem = e.Current; } } PageCount = page; if (PageIndex >= PageCount && PageCount > 0) { pageIndex = PageCount - 1; dataItem = lastItem; } } if (PageCount == 0 && CurrentMode != DetailsViewMode.Insert) { DetailsViewRow row = CreateEmptyRow (); if (row != null) { table.Rows.Add (row); list.Add (row); } } else { // Creates the set of fields to show ICollection fieldCollection = CreateFieldSet (dataItem, dataBinding && dataItem != null); DataControlField [] fields = new DataControlField [fieldCollection.Count]; fieldCollection.CopyTo (fields, 0); foreach (DataControlField field in fields) { field.Initialize (false, this); if (EnablePagingCallbacks) field.ValidateSupportsCallback (); } // Main table creation headerRow = CreateRow (-1, DataControlRowType.Header, DataControlRowState.Normal); DataControlFieldCell headerCell = new DataControlFieldCell (null); headerCell.ColumnSpan = 2; if (headerTemplate != null) headerTemplate.InstantiateIn (headerCell); else if (!String.IsNullOrEmpty (HeaderText)) headerCell.Text = HeaderText; else headerRow.Visible = false; headerRow.Cells.Add (headerCell); table.Rows.Add (headerRow); if (showPager && PagerSettings.Position == PagerPosition.Top || PagerSettings.Position == PagerPosition.TopAndBottom) { topPagerRow = CreateRow (-1, DataControlRowType.Pager, DataControlRowState.Normal); InitializePager (topPagerRow, dataSource); table.Rows.Add (topPagerRow); } foreach (DataControlField field in fields) { DataControlRowState rstate = GetRowState (list.Count); DetailsViewRow row = CreateRow (PageIndex, DataControlRowType.DataRow, rstate); InitializeRow (row, field); table.Rows.Add (row); list.Add (row); } footerRow = CreateRow (-1, DataControlRowType.Footer, DataControlRowState.Normal); DataControlFieldCell footerCell = new DataControlFieldCell (null); footerCell.ColumnSpan = 2; if (footerTemplate != null) footerTemplate.InstantiateIn (footerCell); else if (!String.IsNullOrEmpty (FooterText)) footerCell.Text = FooterText; else footerRow.Visible = false; footerRow.Cells.Add (footerCell); table.Rows.Add (footerRow); if (showPager && PagerSettings.Position == PagerPosition.Bottom || PagerSettings.Position == PagerPosition.TopAndBottom) { bottomPagerRow = CreateRow (-1, DataControlRowType.Pager, DataControlRowState.Normal); InitializePager (bottomPagerRow, dataSource); table.Rows.Add (bottomPagerRow); } } rows = new DetailsViewRowCollection (list); if (dataBinding) DataBind (false); OnItemCreated (EventArgs.Empty); return PageCount; }
AutoGeneratedFieldProperties[] CreateAutoFieldProperties (PagedDataSource source) { if(source == null) return null; PropertyDescriptorCollection props = source.GetItemProperties (new PropertyDescriptor[0]); Type prop_type; ArrayList retVal = new ArrayList(); 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 != null && en.MoveNext ()) { fitem = en.Current; _dataEnumerator = en; } if (fitem != null) prop_type = fitem.GetType(); } if (fitem != null && fitem is ICustomTypeDescriptor) { props = TypeDescriptor.GetProperties(fitem); } else if (prop_type != null) { if (IsBindableType (prop_type)) { AutoGeneratedFieldProperties field = new AutoGeneratedFieldProperties (); ((IStateManager)field).TrackViewState(); field.Name = "Item"; field.DataField = BoundField.ThisExpression; field.Type = prop_type; retVal.Add (field); } else { props = TypeDescriptor.GetProperties (prop_type); } } } if (props != null && props.Count > 0) { foreach (PropertyDescriptor current in props) { if (IsBindableType (current.PropertyType)) { AutoGeneratedFieldProperties field = new AutoGeneratedFieldProperties (); ((IStateManager)field).TrackViewState(); field.Name = current.Name; field.DataField = current.Name; for (int i = 0; i < DataKeyNames.Length; i++) { if (string.Compare (DataKeyNames [i], current.Name, StringComparison.InvariantCultureIgnoreCase) == 0) { field.IsReadOnly = true; break; } } field.Type = current.PropertyType; retVal.Add (field); } } } if (retVal.Count > 0) return (AutoGeneratedFieldProperties[]) retVal.ToArray (typeof(AutoGeneratedFieldProperties)); else return new AutoGeneratedFieldProperties [0]; }
private ArrayList CreateAutoGeneratedFields(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.Static | 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)) { Microsoft.Matrix.Framework.Web.UI.BoundField field = new Microsoft.Matrix.Framework.Web.UI.BoundField(); ((IStateManager) field).TrackViewState(); field.HeaderText = "Item"; field.DataField = Microsoft.Matrix.Framework.Web.UI.BoundField.thisExpr; field.SortExpression = "Item"; field.SetOwner(this); list.Add(field); } else { itemProperties = TypeDescriptor.GetProperties(propertyType); } } } if ((itemProperties != null) && (itemProperties.Count != 0)) { foreach (PropertyDescriptor descriptor in itemProperties) { if (BaseDataList.IsBindableType(descriptor.PropertyType) && ((this.AutoGenerateExcludeFields.Length == 0) || (Array.IndexOf(this.AutoGenerateExcludeFields, descriptor.Name) == -1))) { Microsoft.Matrix.Framework.Web.UI.BoundField field2 = new Microsoft.Matrix.Framework.Web.UI.BoundField(); ((IStateManager) field2).TrackViewState(); field2.HeaderText = descriptor.Name; field2.DataField = descriptor.Name; field2.SortExpression = descriptor.Name; field2.ReadOnly = descriptor.IsReadOnly; field2.SetOwner(this); list.Add(field2); } } } if ((list.Count == 0) && flag) { throw new HttpException(string.Format(Microsoft.Matrix.Framework.SR.GetString("MxDataGrid_NoAutoGenColumns"), this.ID)); } return list; }