namespace WindowsFormsApplication1{ public partial class Myform : Form { public Myform() { InitializeComponent(); Initial(); }
public UiBindList _list { get; set; }
private void Initial() { _list = new UiBindList { SynchronizationContexts = SynchronizationContext.Current }; dataGridView1.DataBindings.Add("DataSource", this, "_list", false, DataSourceUpdateMode.OnPropertyChanged);
new Thread(() => { while (true) { Thread.Sleep(1000); _list.Add(new OBJ { Name = "C#" }); } }) { IsBackground = true, } .Start(); } }
public class UiBindList : BindingList { public SynchronizationContext SynchronizationContexts { get; set; }
public void Excute(Action action, object state = null) { if (SynchronizationContexts == null) action(); else SynchronizationContexts.Post(p => action(), state); }
public new void Add(T item) { Excute(() => base.Add(item)); }
public new void Remove(T item) { Excute(() => base.Remove(item)); } }
public class OBJ { public string Name { get; set; } }
在Add方式中 报错!!对象的当前状态使该操作无效。
4 回答
慕妹3146593
TA贡献1820条经验 获得超9个赞
public new void Add(T item)
{
Excute(() => Add(item));
}
public new void Remove(T item)
{
Excute(() => Remove(item));
}这两个方法去掉base.的引用就好了,改为上面的调用
慕容3067478
TA贡献1773条经验 获得超3个赞
while (true) { Thread.Sleep(1000); _list.Add(new OBJ { Name = "C#" }); }
_list.Add(new OBJ { Name = "C#" });这句没有写括号,_list.Add(new OBJ(){Name="C#"});
- 4 回答
- 0 关注
- 434 浏览
添加回答
举报
0/150
提交
取消