我有一个C# Windows Forms项目,其中包含一个窗体,里面有两个列表框和一个按钮。
在FormLoad时,左侧的ListBox被填充了一个列表(约1800个项目),其中包含有关证券(ID和名称)的信息。当用户点击按钮时,所有证券都将从左边的列表框移动到右边。
如果我不使用BindingSources,也就是直接使用ListBoxes的Items属性,那么这个移动过程需要几秒钟时间:
什么是使用BindingSource的原因导致它需要更长时间?有没有办法使它更快?
如果我不使用BindingSources,也就是直接使用ListBoxes的Items属性,那么这个移动过程需要几秒钟时间:
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时,需要几分钟的时间:
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的原因导致它需要更长时间?有没有办法使它更快?