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

Binding to a collection class
vbNullString
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
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