❔ ✅ WinForms ListBox.DataSource not working
I have a ListBox that contains only instances of Employee. ToString looks like
Employee { Name: "John Smith", Id: 123, Salary: $100,000.00 }
. Without creating my own list and instead just adding objects to the ObjectCollection already owned by the ListBox, everything works. When I create my own list public List<Employee> _employees
and initialize it before the components of the form and set the ListBox data source after the components in the form constructor, no values from the list are shown.
I am refreshing the ListBox in its prompt closing event after adding the Employee to the list. Looking online did not help since they all said to do what I thought I needed to do to bind a ListBox in the first place, which is to just set the data source using the appropriate object or refreshing the list. Here's the relevant code:
7 Replies
Doesn't look too unreasonable to me?
That's what I thought but it didn't work. I figured it out after a few hours though, needed to use a BindingSource for DataSource instead of setting it to the list and call ResetBindings(true) on it instead of Refresh on the ListBox
Use wpf 🙂
I agree but it was for a school assignment
So it had to be a List<T> and had to be winforms
Why not a bindinglist<t>?
I didn’t know those were a thing, I’ll read about those. Thank you
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.