Exemple #1
0
        private void LoadData()
        {
            _people = _dataContext.GetPeople();

            var fPeople = new FilteredBindingList <Person>();

            _people.ForEach(p => fPeople.Add(p));
            _bsPeople.DataSource = fPeople;
        }
        protected override void ApplySortCore(PropertyDescriptor prop,
                                              ListSortDirection direction)
        {
            _sortedList = new ArrayList();

            // Check to see if the property type we are sorting by implements
            // the IComparable interface.
            Type interfaceType = prop.PropertyType.GetInterface("IComparable");

            if (interfaceType != null)
            {
                // If so, set the SortPropertyValue and SortDirectionValue.
                _sortPropertyValue  = prop;
                _sortDirectionValue = direction;

                _unsortedItems = new FilteredBindingList <T>();

                if (_sortPropertyValue != null)
                {
                    // Loop through each item, adding it the the sortedItems ArrayList.
                    foreach (Object item in this.Items)
                    {
                        _unsortedItems.Add((T)item);
                        _sortedList.Add(prop.GetValue(item));
                    }
                }
                // Call Sort on the ArrayList.
                _sortedList.Sort();
                T temp;

                // Check the sort direction and then copy the sorted items
                // back into the list.
                if (direction == ListSortDirection.Descending)
                {
                    _sortedList.Reverse();
                }

                for (int i = 0; i < this.Count; i++)
                {
                    int position = Find(prop.Name, _sortedList[i]);
                    if (position != i && position > 0)
                    {
                        temp           = this[i];
                        this[i]        = this[position];
                        this[position] = temp;
                    }
                }

                _isSortedValue = true;

                // If the list does not have a filter applied,
                // raise the ListChanged event so bound controls refresh their
                // values. Pass -1 for the index since this is a Reset.
                if (String.IsNullOrEmpty(Filter))
                {
                    OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
                }
            }
            else
            {
                // If the property type does not implement IComparable, let the user
                // know.
                throw new InvalidOperationException("Cannot sort by "
                                                    + prop.Name + ". This" + prop.PropertyType.ToString() +
                                                    " does not implement IComparable");
            }
        }