ObjectDisposedException with DataGrid

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.
 
J

Jim Balo

Some more info on this:
I have tried the same scenario with a regular WinForm application &
DataGridView and it works fine there (but using a property for Name instead
of a public variable [thanks Morten!]).

Also, even if I add an item instead of delete, I get the same
ObjectDisposedException:
private void TestForm_Click(object sender, EventArgs e)
{
Person person = new Person("New guy");
_personList.Add(person);
}

What could possibly be causing this?? Any suggestions?

Thanks,
Jim


Ps. Here is an updated sample:

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
{
private string _name = "";
public string Name
{
get { return _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)
{
Person person = new Person("New guy");
_personList.Add(person);
}
}
 
G

Ginny Caughey [MVP]

Jim,

Have you tried unbinding the data to the grid, deleting (or adding) the
object, then rebinding?
 
J

Jim Balo

Yes, I did try that (setting the DataSource to null, adding /deleting, and
then rebinding it again). Did not help.

Jim


Ginny Caughey said:
Jim,

Have you tried unbinding the data to the grid, deleting (or adding) the
object, then rebinding?

--
Ginny Caughey
Device Application Development MVP


Jim Balo said:
Some more info on this:
I have tried the same scenario with a regular WinForm application &
DataGridView and it works fine there (but using a property for Name
instead of a public variable [thanks Morten!]).

Also, even if I add an item instead of delete, I get the same
ObjectDisposedException:
private void TestForm_Click(object sender, EventArgs e)
{
Person person = new Person("New guy");
_personList.Add(person);
}

What could possibly be causing this?? Any suggestions?

Thanks,
Jim


Ps. Here is an updated sample:

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
{
private string _name = "";
public string Name
{
get { return _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)
{
Person person = new Person("New guy");
_personList.Add(person);
}
}
 
G

Ginny Caughey [MVP]

Jim,

Well that's interesting. I can't guess which object is disposed that it's
trying to use.

--
Ginny Caughey
Device Application Development MVP


Jim Balo said:
Yes, I did try that (setting the DataSource to null, adding /deleting, and
then rebinding it again). Did not help.

Jim


Ginny Caughey said:
Jim,

Have you tried unbinding the data to the grid, deleting (or adding) the
object, then rebinding?

--
Ginny Caughey
Device Application Development MVP


Jim Balo said:
Some more info on this:
I have tried the same scenario with a regular WinForm application &
DataGridView and it works fine there (but using a property for Name
instead of a public variable [thanks Morten!]).

Also, even if I add an item instead of delete, I get the same
ObjectDisposedException:
private void TestForm_Click(object sender, EventArgs e)
{
Person person = new Person("New guy");
_personList.Add(person);
}

What could possibly be causing this?? Any suggestions?

Thanks,
Jim


Ps. Here is an updated sample:

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
{
private string _name = "";
public string Name
{
get { return _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)
{
Person person = new Person("New guy");
_personList.Add(person);
}
}
 
J

Jim Balo

Hi Ginny,
Well that's interesting. I can't guess which object is disposed that it's
trying to use.

Neither can I - very strange. I appreciate your suggestion earlier. If you
have time, you could try the sample I posted and see if you get the same
phenomenon (but I understand if you do not have time to do this). I can't
help thinking that I am missing something really obvious (I am new to
compact .NET development).

Thanks,
Jim
 
J

Jim Balo

I found a solution to the problem: If I set the DataSource of the DataGrid
to null in either Form_Closing or Dispose, it works fine.

It appears that the DataGrid does not get disposed properly unless this is
done (even when using a "using" construct and explicitly calling
GC.Collect()).

This is probably a bug with in the DataGrid.

Jim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top