Using BindingSource is very slow?(使用 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 很慢?


- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 输入按键事件处理程序 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04