/// <summary>
        /// Handles adding an item into the collection, and applying sorting, filtering, grouping, paging.
        /// </summary>
        /// <param name="item">Item to insert in the collection</param>
        /// <param name="index">Index to insert item into</param>
        private void ProcessInsertToCollection(object item, int index)
        {
            // first check to see if it passes the filter
            if (this.Filter == null || this.PassesFilter(item))
            {
                if (this.SortDescriptions.Count > 0)
                {
                    // create the SortFieldComparer to use
                    SortFieldComparer sortFieldComparer = new SortFieldComparer(this.SortDescriptions);

                    // check if the item would be in sorted order if inserted into the specified index
                    // otherwise, calculate the correct sorted index
                    if (index < 0 || /* if item was not originally part of list */
                        (index > 0 && (sortFieldComparer.Compare(item, this.InternalItemAt(index - 1)) < 0)) || /* item has moved up in the list */
                        ((index < this.InternalList.Count - 1) && (sortFieldComparer.Compare(item, this.InternalItemAt(index)) > 0))) /* item has moved down in the list */
                    {
                        index = sortFieldComparer.FindInsertIndex(item, this._internalList);
                    }
                }

                // make sure that the specified insert index is within the valid range
                // otherwise, just add it to the end. the index can be set to an invalid
                // value if the item was originally not in the collection, on a different
                // page, or if it had been previously filtered out.
                if (index < 0 || index > this._internalList.Count)
                {
                    index = this._internalList.Count;
                }

                this._internalList.Insert(index, item);
            }
        }
Ejemplo n.º 2
0
 private void ProcessInsertToCollection(object item, int index)
 {
     if ((this.Filter == null) || this.PassesFilter(item))
     {
         if (this.SortDescriptions.Count > 0)
         {
             SortFieldComparer comparer = new SortFieldComparer(this.SortDescriptions);
             if (((index < 0) || ((index > 0) && (comparer.Compare(item, this.InternalItemAt(index - 1)) < 0))) || ((index < (this.InternalList.Count - 1)) && (comparer.Compare(item, this.InternalItemAt(index)) > 0)))
             {
                 index = comparer.FindInsertIndex(item, this._internalList);
             }
         }
         if ((index < 0) || (index > this._internalList.Count))
         {
             index = this._internalList.Count;
         }
         this._internalList.Insert(index, item);
     }
 }