Sorted ListBox with Custom Collection DataSource

  • Thread starter Thread starter Andreas Zita
  • Start date Start date
A

Andreas Zita

Hi

I have a ListBox with a Custom Collection (Customers) as DataSource. The
collection implements IBindingList, but only the change notification part.
When changing a customers name in the collection I also sort the list using
InnerList.Sort(). The problem occurs when a certain item is selected int the
listbox while Im changing the name from a bound textbox in the same form.
This triggers the Sort() but apparently the bindingcontext is still focused
on the same position, which now is a completely different customer! How can
I make the bindingcontext to stay updated with the position of the selected
item?

/Andreas Zita
 
Hi,

Andreas Zita said:
Hi

I have a ListBox with a Custom Collection (Customers) as DataSource. The
collection implements IBindingList, but only the change notification part.
When changing a customers name in the collection I also sort the list
using InnerList.Sort(). The problem occurs when a certain item is selected
int the listbox while Im changing the name from a bound textbox in the
same form. This triggers the Sort() but apparently the bindingcontext is
still focused on the same position, which now is a completely different
customer! How can I make the bindingcontext to stay updated with the
position of the selected item?

What does your code look like before & after InnerList.Sort() ? Are you
using ListChangedType.ItemMoved like :

public class CustomList : CollectionBase, IBindingList
{
// ...
internal void OnItemChanged(CustomObject o)
{
int oldIdx = List.IndexOf(o);

if ( ListChanged!=null )
ListChanged(this, new ListChangedEventArgs(
ListChangedType.ItemChanged, oldIdx));

InnerList.Sort();

int newIdx = List.IndexOf(o);

if ( ListChanged!=null && newIdx!=oldIdx )
ListChanged(this, new ListChangedEventArgs(
ListChangedType.ItemMoved, newIdx, oldIdx));
}
// ...
}

HTH,
Greetings
 
Thanks! Of course! No I was doing it a bit different... but your way worked
much better.

/Andreas Zita
 
Back
Top