nog*_*ola 4 .net listbox bindingsource winforms
我有一个C#Windows窗体项目,其中包含一个包含2个ListBox和一个按钮的Form.在FormLoad上,左侧ListBox填充了一个列表(大约1800个项目),其中包含有关证券(ID和名称)的信息,当用户点击按钮时,所有证券都从左侧列表框移动到右侧.
当我没有使用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);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我使用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);
}
}
Run Code Online (Sandbox Code Playgroud)
BindingSource方式需要更长时间的原因是什么?有没有办法让它更快?
在对BindingSource执行大量更改之前,应该取消设置BindingSource上的RaiseListChangedEvents属性,并在完成后重置.然后,您可以使用ResetBindings刷新绑定控件.
您还应该使用BeginUpdate/EndUpdate在列表框项目上包装大量操作,以避免重绘.这很可能是导致经济放缓的主要因素.
| 归档时间: |
|
| 查看次数: |
4069 次 |
| 最近记录: |