Possible Duplicate:
C# Force ListBox to update elements
Let's consider this small piece of code:
listbox.DataSource = base_items;
listbox.DisplayMember = "Name";
// ... a bit later in the program
base_items.Add( new Base_Item("Unnamed") );
From this point, how do I do to make the listbox update its items? The only way for me to see the update is to close the window and reload it again.
Just remove and add databinding again.
You can create method that can be used on first load and when new item was added:
void BindData()
{
listBox.DataSource = null;
listBox.DataSource = base_items;
listbox.DisplayMember = "Name";
}
So here is the code for adding new item and refreshing listbox:
base_items.Add(new Base_Item("Unnamed"));
BindData();
As mentioned in Marc G's answer.
C# Force ListBox to update elements
If you know that the list needs refreshing, simple update the DisplayMember
of the listbox.
listbox.DisplayMember = "";
listbox.DisplayMember = "Name";