Binding to a collection class

I have read Rockford Lhotka's excellent article on binding to obejects - 
http://msdn.microsoft.com/library/default.asp url=/library/en-us/dnadvnet/html/vbnet02252003.asp

But Searching was specifically not supported in the example. Nor is it in the Microsoft IBindingList example. Does anyone know of a sample where searching and sorting are supported


Answer this question

Binding to a collection class

  • vbNullString

    I don't know of one, but it'd be pretty straightforward.  The problem is that it could vary tons depending on what your underlying data looks like.  In the case where the data isn't sorted, you probably can't do better than walking through the elements ("You   No.  You   No.  You   Yes.")

    It'd look something like this (I didn't test this, but something like this should work -- and of course you'd set SupportsSearching/SupportsSorting to true).  Especially for sorting, this will vary a lot depending on what your underlying data is.  You may get lucky and have something that will sort itself (like an array).   If the elements are IComparable, you probably want to use IComparable.Compare.  Also note that the sort that's used here is terrible (but is easy to understand, and would be ok for a small number of elements).  If you have a large number of elements, check out QuickSort, especially if you're going to sort often.  (Last side note (honest) -- QuickSort is really bad when the list has to be reversed.  You might consider checking for this explicitly.  If you're reversing the list, you could swap element 0 with count-1, 1 with count-2, and so on until you get halfway through the list).

    -Scott

    public int Find(System.ComponentModel.PropertyDescriptor property, object key)
    {
    object val; //the value of item #n
    for (int i=0; i<myList.Count; i++) 
    {
    val = property.GetValue(myList[i]);
    if (val == key) 
    return i; //Got it...
    }
    return -1; //Didn't get it.
    }

    PropertyDescriptor sortedProperty = null; //This is our sort property...
    ListSortDirection sortedDirection = ListSortDirection.Ascending; //...and direction
    public void ApplySort(System.ComponentModel.PropertyDescriptor property, System.ComponentModel.ListSortDirection direction)
    {
    //Store how we're sorting, so you can retrieve the values later...
    sortedProperty = property;
    sortedDirection = direction;
    bool goodOrder; //indicates that the first value in the list should be before the second
    //Slow sort
    for (int i=0; i<myList.Count - 1; i++)
    for (int j=i+1; j<myList.Count; j++) 
    {
    //Need to know what kind of data this is to compare it.  I just went with int.
    goodOrder = ((int)property.GetValue(myList[i]) < (int)property.GetValue(myList[j]));
    if (direction == ListSortDirection.Descending)
    goodOrder = !goodOrder; //We're descending, so reverse direction
    if (!goodOrder) 
    {
    object temp = myList[i];
    myList[i] = myList[j];
    myList[j] = temp;
    }
    }
    ListChanged(this, new ListChangedEventArgs(ListChangedType.Reset, -1, -1);
    }

    public void RemoveSort()
    {
    //In our case, we don't do much of anything here -- we don't have an "unsort" concept.
    sortedProperty = null;
    sortedDirection = ListSortDirection.Ascending;
    }

    public System.ComponentModel.PropertyDescriptor SortProperty
    {
    get
    {
    return sortedProperty;
    }
    }

    public bool IsSorted
    {
    get
    {
    return (sortedProperty != null);
    }
    }

  • James 1

    I think that you are looking for the IBindingList::ApplySort(PropertyDescriptor property, ListSortDirection direction) and for the IBindingList::Find(PropertyDescriptor property, object key) methods.

    IBindingList::ApplySort signals the list that it should be sorted and IBindingList::Find tells the list to return the index of the "key".

    Hope this helps,
    Daniel.

  • kanguru

    Specifically I want to bind to a class of objects, sorted on a property of the class and be able to retrieve an object by using a key which is another property of the object. I also want to change the sort property of an object and have my reflect the new order. Is that possible  My class (in VB) Inherits CollectionBase and Implements IBindingList.
  • Binding to a collection class