Is using BindingSource really slow?

I have a C # Windows Forms project with a form containing 2 ListBoxes and a button. In FormLoad, the left ListBox is filled with a list (about 1800 elements) containing information about the securities (identifier and name), and when the user clicks on the button, all securities are moved from the left list to the right.

When I do not use BindingSources, that is, I directly use the Items property in ListBoxes, the move 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);
    }
}

But, when I use BindingSources, it takes a few 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);
    }
}

What is the reason that the BindingSource path will take a lot longer? Is there any way to make this faster?

+3
3

, . , reset . :)

0

RaiseListChangedEvents BindingSource, BindingSource reset, . ResetBindings .

BeginUpdate/EndUpdate, . , .

+6

listBox1.DataSource = bindingSource1;
listBox2.DataSource = bindingSource2;

...

private void button1_Click(object sender, EventArgs e)
{
      listBox2.DataSource = listBox1.DataSource;
}
0

Source: https://habr.com/ru/post/1781969/


All Articles