修改带有过滤数据源的 BindingSource.Current 反映了对原始数据源的更改,但 BindingSource.RemoveCurrent() 不是:为什么?

时间:2021-01-13 15:12:19

标签: c# .net data-binding datasource bindingsource

假设我有以下对象:

MyCustomObject {
        public int Id { get; set; } = new Random().Next();
        public string Name { get; set; }
    }

列表

private List<MyCustomObject> _mainList = new List<MyCustomObject>();

和绑定源

private BindingSource _mainBindingSource = new BindingSource();

然后,我将过滤列表定义为 BindingSource 的数据源

_mainBindingSource.DataSource = _mainList.Where(x => x.Name.Contains(filterInput.Text));

我的问题:

为什么有效

(_mainBindingSource.Current as MyCustomObject).Name = "Foo" // It also changes the object on _mainList

不是吗?

_mainBindingSource.RemoveCurrent(); // The item is removed from _mainBindingSource but not from _mainList

当我调用 _mainBindingSource.RemoveCurrent();使用未过滤的列表作为数据源,该项目也会从该列表中删除。

0 个答案:

没有答案