Click here to Skip to main content
15,893,790 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a asynchronous bindinglist that I got from here:windowsclient.net/articles/asyncbindinglist.aspx and I also override ApplySortCore, PropertyDescriptor, etc...in order to allow the items to be sorted within a dataGridView.

I've binded this control to a dataGridView but I seem to throw Index out of Range exceptions at random when I add or delete items from my dataGridView.

here it code:

C#
    [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);
        }
    }
}


Can someone please point out what I'm doing wrong here?
Posted
Updated 13-Jun-12 13:02pm
v2
Comments
Nelek 13-Jun-12 19:03pm    
Code tags added.

For the next time... it is <pre> and </pre>, not [code] and [/code]
d.allen101 13-Jun-12 20:00pm    
thx nelek! i'll remember that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900