c# - 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();
谢谢!!!!!
解决方案
推荐阅读
- python - Python/Numpy 每第 n 行切片一个数组
- mysql - "SELECT *" 除了不允许用户查看的列
- javascript - 如何使用 Dropzone.js 进行分块文件上传(仅限 PHP)?
- oracle - Oracle 数据库 - 授予/撤销高并发
- networking - BrowserSync:不工作外部链接
- firefox - 如何在 Firefox 的数据 URI 中生成哈希 (#)?
- javascript - fetch 和 XMLHttpRequest 的问题
- keyframe - 为什么当我们在时间轴中添加或删除顶点后形状会发生变化
- java - Optaplanner 中的建模活动调度
- java - 正则表达式不起作用将拖尾报价