首页 > 解决方案 > C#如何从bindingSource中删除不断更新的项目(数据)

问题描述

我正在使用一个 datagridView,它绑定到一个bindingsource,这个绑定源是一个数组List<MyClass>。从bindingsource其他线程不断更新(每 100 毫秒)。

* other threads can remove, add or update the bindingsource in any give time.

1)该bindingsource.Remove(object)方法没有删除对象,因为属性值不同,因此删除不匹配。

2)bindingsource.RemoveAt(int)确实删除了对象,但在某些情况下是错误的对象,因为bindingsource索引一直在被其他线程更新。

我可以从bindingsource按属性名称中删除对象吗?或者如何删除正确的对象?

下面的代码是不工作的 RemoveAt

        dataGridView_manage_positions.SuspendLayout();
        var position = GetPosition(uniqueID);
        int index = 0;
        for (int i = 0; i < _bindingSource_manage_grid.Count; i++)
        {
            var row = (MyClass)_bindingSource_manage_grid[i];
            // EXAMPLE: the row.Net = 1.52
            if (row.ID != position.ID)
                continue;

            dataGridView_manage_positions.BeginInvoke(new Action(() =>
            {
                 // this the problem. HERE i have a race condition 
                 // EXAMPLE: the row.Net = 2.56
                 // now the Remove() will not work, because the object value is different
                _bindingSource_manage_grid.Remove(row);
            }));

            break;
        }

            dataGridView_manage_positions.ResumeLayout();

谢谢!!!!!

标签: c#multithreadingdatasource

解决方案


推荐阅读