[Serializable] public class AsyncBindingList<t> : BindingList<t> { [NonSerialized] bool isSortedValue; [NonSerialized] ListSortDirection sortDirectionValue; [NonSerialized] PropertyDescriptor sortPropertyValue; [NonSerialized] ArrayList sortedList; [NonSerialized] ArrayList unsortedItems; [NonSerialized] ISynchronizeInvoke control; public AsyncBindingList(ISynchronizeInvoke control) { this.control = control; } delegate void ListChangedDelegate(ListChangedEventArgs e); protected override void OnListChanged(ListChangedEventArgs e) { if (control.InvokeRequired) { //IAsyncResult asyncResult = invoke.BeginInvoke(new ListChangedDelegate(base.OnListChanged), new object[] { e }); control.Invoke(new ListChangedDelegate(base.OnListChanged), new object[] { e }); } else base.OnListChanged(e); } protected override bool SupportsSearchingCore { get { return true; } } protected override bool SupportsSortingCore { get { return true; } } protected override bool IsSortedCore { get { return isSortedValue; } } protected override PropertyDescriptor SortPropertyCore { get { return sortPropertyValue; } } protected override ListSortDirection SortDirectionCore { get { return sortDirectionValue; } } protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction) { sortedList = new ArrayList(); // check to see if the property implements IComparable interface Type interfaceType = prop.PropertyType.GetInterface("IComparable"); if (interfaceType != null) { sortPropertyValue = prop; sortDirectionValue = direction; unsortedItems = new ArrayList(this.Count); foreach (object item in this.Items) { sortedList.Add(prop.GetValue(item)); unsortedItems.Add(item); } sortedList.Sort(); T temp; if (direction == ListSortDirection.Descending) sortedList.Reverse(); for (int i = 0; i < this.Count; i++) { int pos = Find(prop.Name, sortedList[i]); if (pos != i) { temp = this[i]; this[i] = this[pos]; this[pos] = temp; } } isSortedValue = true; OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1)); } else throw new NotSupportedException("Cannot sort by " + prop.Name ); } protected override int FindCore(PropertyDescriptor prop, object key) { // Get the property info for the specified property. System.Reflection.PropertyInfo propInfo = typeof(T).GetProperty(prop.Name); T item; if (key != null) { // Loop through the items to see if the key // value matches the property value. for (int i = 0; i < Count; ++i) { item = (T)Items[i]; if (propInfo.GetValue(item, null).Equals(key)) return i; } } return -1; } public int Find(string property, object key) { // Check the properties for a property with the specified name. PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T)); PropertyDescriptor prop = properties.Find(property, true); // If there is not a match, return -1 otherwise pass search to // FindCore method. if (prop == null) return -1; else return FindCore(prop, key); } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)