开发者

ListBox List<T> binding issue when removing item

开发者 https://www.devze.com 2023-02-13 18:12 出处:网络
I\'m having trouble with a ListBox control that has a List of objects binded to it. I\'m calling this function each time I add or remove an object to/from the list:

I'm having trouble with a ListBox control that has a List of objects binded to it.

I'm calling this function each time I add or remove an object to/from the list:

private void RefreshBarList()
{
   lstBar.DataSource = null;
   lstBar.Items.Clear();
   lstBar.DisplayMember = "BarDescription";
   lstBar.DataSource = BarCollection.BarList;
   lstBar.Refresh();
}

It works well when adding items to the list or removing anything but the last item in the list. If i remove the last object in the list it will display in the listbox the object types and not the BarDescription.

BarCollection is another class that contains a List with Bar objects a开发者_Go百科nd BarCollection.BarList returns exactly that list.

The Bar class is very simple:

public class Bar
{
   private string _barDescription;

   public string BarDescription
   {
      get
      {
         return _barDescription;
      }
   }

   public Bar(string barDescription)
   {
      _barDescription = barDescription;
   }
}

I cannot figure out why this happens, does anyone have any suggestions? Any help will be appreciated.


Try using a BindingList<T> instead of a List<T>


I had the same problem, only put ToArray() in mylist and solved the problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消