使用 BindingSource 很慢?

1

本文介绍了使用 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 很慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

参数计数与调用不匹配?
Parameter count mismatch with Invoke?(参数计数与调用不匹配?)...
2023-11-11 C#/.NET开发问题
26

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14

具有未知类型的 CreateDelegate
CreateDelegate with unknown types(具有未知类型的 CreateDelegate)...
2023-11-11 C#/.NET开发问题
5

Func&lt;T&gt;.BeginInvoke 使用线程池吗?
Does Funclt;Tgt;.BeginInvoke use the ThreadPool?(Funclt;Tgt;.BeginInvoke 使用线程池吗?)...
2023-11-11 C#/.NET开发问题
6