你如何正确地更新从后台线程数据绑定的DataGridView [英] How do you correctly update a databound datagridview from a background thread
问题描述
我有实现INotifyPropertyChanged的自定义对象。我的这些对象的集合,其中所述集合是基于的BindingList
我创建了一个收集绑定源,并设置的BindingSource和DataGridView的数据源。
I have a custom object that implements INotifyPropertyChanged. I have a collection of these objects where the collection is based on BindingList I have created a binding source for the collection, and set the datasources of the bindingsource and datagridview.
一切都很正常,但我需要更新从后台线程自定义对象的属性。当我这样做,我得到以下错误:
Everything works great, except I need to update properties on the custom object from background threads. when I do so, I get the following error :
的BindingSource不能是它自己的数据源。不要设置数据源和DataMember属性值的参考,回到BindingSource的
BindingSource cannot be its own data source. Do not set the DataSource and DataMember properties to values that refere back to BindingSource
我发现下面的职位,似乎有我确切的问题(和解决方案?),但我不能完全看着办吧。
I found the following post that seems to have my exact problem (and solution?) but I cannot quite figure it out.
<一个href=\"http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/3566f7c7-eb47-422e-ab09-9549a18da360/\">http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/3566f7c7-eb47-422e-ab09-9549a18da360/
我创建,并在我的业务对象初始化每个职位的OPER变量,然后我把两个事件功能为我的集合类。这正确编译,但无一例外挂起时运行。
I created and initialized the oper variables per the post in my business object, and then I put the two event functions into my collection class. This compiled correctly, but hangs without exception when run.
我看过很多帖子说要使用调用/调用开始,但我不呼吁UI任何功能,只是更新业务对象,所以我不知道,我会把调用呼叫。
I have seen many posts saying to use Invoke/Begin Invoke, but I am not calling any functions on the UI, just updating business objects, so I am not sure where I would put the invoke calls.
一个限制:我想要的业务对象仍然不知道谁正在显示它的(因为有多个消费者)的GUI引用,这样发送到业务对象,这样我以后能够调用使用这些引用调用不是选项。
One restriction : I want the business object to remain unaware of who is displaying it (as there are multiple consumers) so sending in GUI references into the business object so that I am later able to call invoke using those references is not an option.
推荐答案
我在工作的论坛,发现这个类。只需用这个代替的BindingList
I found this class in a forum that works. Just use this instead of BindingList
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Threading;
namespace Utility
{
public class ThreadedBindingList<T> : BindingList<T>
{
SynchronizationContext ctx = SynchronizationContext.Current;
protected override void OnAddingNew(AddingNewEventArgs e)
{
if (ctx == null)
{
BaseAddingNew(e);
}
else
{
ctx.Send(delegate
{
BaseAddingNew(e);
}, null);
}
}
void BaseAddingNew(AddingNewEventArgs e)
{
base.OnAddingNew(e);
}
protected override void OnListChanged(ListChangedEventArgs e)
{
// SynchronizationContext ctx = SynchronizationContext.Current;
if (ctx == null)
{
BaseListChanged(e);
}
else
{
ctx.Send(delegate
{
BaseListChanged(e);
}, null);
}
}
void BaseListChanged(ListChangedEventArgs e)
{
base.OnListChanged(e);
}
}
}
这篇关于你如何正确地更新从后台线程数据绑定的DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!