internal static void AddListBoxItems(ListBox listBox1) { var Items = new FilteredBindingList <Item>(); foreach (var Item in Constants.Items.Values) { Items.Add(Item); } listBox1.DataSource = new BindingSource(Items, null); listBox1.Sorted = true; }
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. var 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) { foreach (object item in 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 (var i = 0; i < Count; i++) { var 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 + " does not implement IComparable"); } }