ListBox.DataSource: How to reverse the order?

glasswizzard

Well-known member
Joined
Nov 22, 2019
Messages
126
Programming Experience
Beginner
Hello, I'm populating a ListBox by setting it as the DataSource for a List of strings. Everything is working fine but the ListBox is populated in reverse order (new items are added at the bottom). Is there a way to change this?

Thanks
 
Last edited:
I'm populating a ListBox by setting it as the DataSource for a List of strings.
Presumably you mean the other way around.

If you bind a list to a control then the control will display the items in the same order they are in the bound list. If you call the Add method then an item will be appended to the list, so the control will reflect that. If you want an item prepended then you would need to call Insert instead, specifying zero as the index at which to insert.

If you want to bind a list to a control and make changes afterwards, don't use a List<T>. Use a BindingList<T> instead. The latter is built for binding specifically, meaning that the bound control will update automatically as you add, remove and set items. That won't happen with a List<T>.
 
If you actually mean that you want the existing items to be displayed in reverse order then, again, I say that the control will reflect the data source, so you'd need a data source in the reverse order. That means that you would have to either reverse the existing list in place or create a new list in the reverse order and bind that instead. Both are easily done.
 
JM, thanks for the help, I do apologize though, because whatever the reason was that things where going into the listbox in reverse order (the reverse order of the datasource) has fixed itself, it's working now exactly as it should, strange, but I'm not complaining :)

I have updated my List to a BindingList<T> as you suggested, thanks for that.

Mods, this is a pointless thread now since the problem solved itself (why can't all problems be like that?) so feel free to delete it.
 
Aha! I figured out how to recreate what was going wrong with the ordering of the listbox. What happens is when the listbox isn't showing (it's form is not open) everything goes into the list normally (new text goes at the top of the list), but if I have the listbox showing as I add items to it, then they go to the bottom of the list instead. It's not a major issue, if there's no simple workaround.
 
Aha! I figured out how to recreate what was going wrong with the ordering of the listbox. What happens is when the listbox isn't showing (it's form is not open) everything goes into the list normally (new text goes at the top of the list), but if I have the listbox showing as I add items to it, then they go to the bottom of the list instead. It's not a major issue, if there's no simple workaround.
That doesn't make sense. Newly added items showing at the bottom of the list IS normal. If you provide actual instructions on how to reproduce the behaviour you're seeing then maybe we can tell you what's actually going on but if you're binding a collection to a ListBox then you should see the items in the same order as they are in the collection and that should be most recently added at the bottom.
 
That is of course unless you are specifying them to be sorted from bottom to top first. Without seen the code for how they are added to the new list, its hard to say. Or if you have sorted them in the source location, they too will fill your new list as they are also stored at the location of your sources collection. Have you done any custom sorting to either of your collections?
 
Back
Top Bottom