Sorted ListBox with Custom Collection DataSource

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
 
B

Bart Mermuys

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
 
A

Andreas Zita

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

/Andreas Zita
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top