質問

So I've got the existing code which works just fine when using the BindingList -> BindingSource -> Windows Control approach:

_items.Clear();
foreach (Item item in GetItems())
{
   _items.Add(item);
}

Where _items is module level instance of BindingList.

The above code works as it should, in that the grid control I'm using correctly displays the items after they have been added each time.

However, I'm concerned about performance, specifically, having to iterate over each of the items again. I would much prefer code like this:

_items = new BindingList<Item>(GetItems());

But it's not updating the grid with the new items as did the previously mentioned block of code. What am I missing?

役に立ちましたか?

解決

With re-initialization, you are breaking the binding link in between. In case you want to update the grid, you have to reassign the broken link as well.

For example, in case its ItemsSource you are binding to, you need to reset that value:

_items = new BindingList<Item>(GetItems());
grid.ItemsSource = _items;
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top