C
C#13mo ago
DaVinki

❔ ✅ 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:
public partial class Form1 : Form
{
private List<Employee> _employees;
public Form1()
{
_employees = new();
InitializeComponent();
EmployeesListBox.DataSource = _employees;
}

private void CreateEmployeeButton_Click(object sender, EventArgs e)
{
var employee = new Employee();
var prompt = new EmployeeCreatorForm(employee);
prompt.Closing += (_, _) => // No leaking since prompt is closing
{
if (!prompt.Completed)
return;

_employees.Add(employee);
Show();
EmployeesListBox.Refresh();
};

Hide();
prompt.Show();
}

private void CreateManagerButton_Click(object sender, EventArgs e)
{
var manager = new Manager();
var prompt = new ManagerCreatorForm(manager);
prompt.Closing += (_, _) =>
{
if (!prompt.Completed)
return;

_employees.Add(manager);
Show();
EmployeesListBox.Refresh();
};

Hide();
prompt.Show();
}
}
public partial class Form1 : Form
{
private List<Employee> _employees;
public Form1()
{
_employees = new();
InitializeComponent();
EmployeesListBox.DataSource = _employees;
}

private void CreateEmployeeButton_Click(object sender, EventArgs e)
{
var employee = new Employee();
var prompt = new EmployeeCreatorForm(employee);
prompt.Closing += (_, _) => // No leaking since prompt is closing
{
if (!prompt.Completed)
return;

_employees.Add(employee);
Show();
EmployeesListBox.Refresh();
};

Hide();
prompt.Show();
}

private void CreateManagerButton_Click(object sender, EventArgs e)
{
var manager = new Manager();
var prompt = new ManagerCreatorForm(manager);
prompt.Closing += (_, _) =>
{
if (!prompt.Completed)
return;

_employees.Add(manager);
Show();
EmployeesListBox.Refresh();
};

Hide();
prompt.Show();
}
}
7 Replies
Mayor McCheese
Mayor McCheese13mo ago
Doesn't look too unreasonable to me?
DaVinki
DaVinkiOP13mo ago
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
Mayor McCheese
Mayor McCheese13mo ago
Use wpf 🙂
DaVinki
DaVinkiOP13mo ago
I agree but it was for a school assignment So it had to be a List<T> and had to be winforms
Mayor McCheese
Mayor McCheese13mo ago
Why not a bindinglist<t>?
DaVinki
DaVinkiOP13mo ago
I didn’t know those were a thing, I’ll read about those. Thank you
Accord
Accord13mo ago
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.
Want results from more Discord servers?
Add your server