J
Jim Balo
Hi,
I am at my wits end here. I am getting ObjectDisposedException when
removing an item from a BindingList when it is bound to a DataGrid. Here is
a simple example to reproduce:
private void Test()
{
Person personA = new Person("Pete");
Person personB = new Person("James");
BindingList<Person> persons = new BindingList<Person>();
persons.Add(personA);
persons.Add(personB);
using (TestForm form = new TestForm(persons))
{
form.ShowDialog();
}
using (TestForm form = new TestForm(persons))
{
form.ShowDialog();
}
}
public class Person
{
public string Name = "";
public Person(string name)
{
Name = name;
}
}
public partial class TestForm : Form
{
public BindingList<Person> _personList = null;
public TestForm(BindingList<Person> personList)
{
InitializeComponent();
_personList = personList;
}
private void TestForm_Load(object sender, EventArgs e)
{
uxTestDataGrid.DataSource = _personList;
}
private void TestForm_Click(object sender, EventArgs e)
{
_personList.RemoveAt(0);
}
}
In TestForm, I remove the first item from the _personList in the Click
event.
As you can see, I am displaying the TestForm twice. The first time,
removing an item work fine. The second time, I get ObjectDisposedException.
Whether or not I remove an item the first time TestForm is shown does not
make a difference.
I must be missing something obvious here, but it eludes me.
Any help would be greatly appreciated.
Jim
Ps. If I do not bind _personList to the DataGrid, it works just fine. And
binding it to a Combobox instead works fine. Lastly, binding a simple
List<Person> (as opposed to BindingList<Person>) to the DataGrid works fine
as well.
I am at my wits end here. I am getting ObjectDisposedException when
removing an item from a BindingList when it is bound to a DataGrid. Here is
a simple example to reproduce:
private void Test()
{
Person personA = new Person("Pete");
Person personB = new Person("James");
BindingList<Person> persons = new BindingList<Person>();
persons.Add(personA);
persons.Add(personB);
using (TestForm form = new TestForm(persons))
{
form.ShowDialog();
}
using (TestForm form = new TestForm(persons))
{
form.ShowDialog();
}
}
public class Person
{
public string Name = "";
public Person(string name)
{
Name = name;
}
}
public partial class TestForm : Form
{
public BindingList<Person> _personList = null;
public TestForm(BindingList<Person> personList)
{
InitializeComponent();
_personList = personList;
}
private void TestForm_Load(object sender, EventArgs e)
{
uxTestDataGrid.DataSource = _personList;
}
private void TestForm_Click(object sender, EventArgs e)
{
_personList.RemoveAt(0);
}
}
In TestForm, I remove the first item from the _personList in the Click
event.
As you can see, I am displaying the TestForm twice. The first time,
removing an item work fine. The second time, I get ObjectDisposedException.
Whether or not I remove an item the first time TestForm is shown does not
make a difference.
I must be missing something obvious here, but it eludes me.
Any help would be greatly appreciated.
Jim
Ps. If I do not bind _personList to the DataGrid, it works just fine. And
binding it to a Combobox instead works fine. Lastly, binding a simple
List<Person> (as opposed to BindingList<Person>) to the DataGrid works fine
as well.