Mysterious BindingList linkages (.NET 2.0)

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is there a reason why the BindingList constructor doesn't create a new
BindingList?

Create a form with four ListBoxes and the following will result in them all
containing the exact same thing. The expected behavior would be for each of
the first three to contain different sets numbers.

public Form1()
{
InitializeComponent();

BindingList<string> l1 = new BindingList<string>();
l1.Add("1");
l1.Add("2");
l1.Add("3");
listBox1.DataSource = l1;

BindingList<string> l2 = new BindingList<string>(l1);
listBox2.DataSource = l2;

BindingList<string> l3 = new BindingList<string>(l1);
listBox3.DataSource = l2;

l1.Remove("1");
l2.Remove("2");

listBox4.DataSource = l1;
}
 
Hi

Based on my understanding, the method below means the BindingList will
create a new instance based on the IList instance.
So that we do not need to add the items in the IList into BindingList one
by one.
BindingList (Generic IList) Initializes a new instance of the BindingList
class with the specified list.
Supported by the .NET Compact Framework.

Also I think you want a Clone method, which is not implemented in the
BindingList so far.

So far you may try the code below.
string[] strs = new string[l1.Count];
l1.CopyTo(strs,0);
List<string> s = new List<string>(strs);
BindingList<string> l2 = new BindingList<string>(s);
listBox2.DataSource = l2;

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top