使用 BindingSource 很慢?

Using BindingSource is very slow?(使用 BindingSource 很慢?)

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

问题描述

我有一个 C# Windows 窗体项目,其中包含一个包含 2 个列表框和一个按钮的窗体.在 FormLoad 上,左侧 ListBox 填充了一个列表(大约 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 很慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用 BindingSource 很慢?

基础教程推荐