使用BindingSource很慢吗? [英] Using BindingSource is very slow?

查看:272
本文介绍了使用BindingSource很慢吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#Windows Forms项目,该项目的Form包含2个ListBoxes和一个按钮. 在FormLoad上,左侧的列表框填充了一个列表(约1800个项目),其中包含有关证券(ID和名称)的信息,并且当用户单击按钮时,所有证券都从左侧的列表框移至右侧.

I have a C# Windows Forms project with a Form containing 2 ListBoxes and a button. On FormLoad the left ListBox is filled with a list (about 1800 items) containing information about Securities (ID and Name) and when the user clicks on the button all the securities are moved from the left listbox to the right.

当我不使用BindingSources时,即我直接使用ListBoxes的Items属性,移动过程将花费几秒钟:

When I'm not using BindingSources, i.e. I'm directly using the Items property of the ListBoxes the moving process takes a few seconds:

private void button1_Click(object sender, EventArgs e)
{
    while (listBox1.Items.Count > 0)
    {
         Security s = listBox1.Items[0] as Security;
         listBox1.Items.Remove(s);
         listBox2.Items.Add(s);
    }
}

但是,当我使用BindingSources时,需要花费几分钟:

But, when I'm using BindingSources it takes several minutes:

listBox1.DataSource = bindingSource1;
listBox2.DataSource = bindingSource2;

...

private void MainForm_Load(object sender, EventArgs e)
{
    ICollection<Security> securities = GetSecurities();
    bindingSource1.DataSouce = securities;
}

private void button1_Click(object sender, EventArgs e)
{
    while (bindingSource1.Count > 0)
    {
        bindingSource1.Remove(s);
        bindingSource2.Add(s);
    }
}

BindingSource方式花费这么长时间的原因是什么? 有什么方法可以使其更快?

What's the reason for the BindingSource-way to take so much longer? Is there any way to make it faster?

推荐答案

好,解决了. 我必须操纵基础集合,然后最后重置绑定.现在,它几乎立即移动了:)

Ok, solved it. I have to manipulate the underlying collection and then reset bindings at the end. Now it's almost instantly moving :)

这篇关于使用BindingSource很慢吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆