The last 3 weeks I have experienced many problems with the DataGridView. I'm using NHibernate 2.1.2 to retrieve objects from a database and I use the DataGridView to represent this data in a structured way. I've been able to isolate the problem in a test-case.
So here's the test-case:
I have an object called Person. This Person has a collection of PersonCars. A PersonCar consists out of a CarType and a Registration Number. Very basic so far since it's a test-case anyway :)
When I retrieve a Person from the database with already existing PersonCars I can add new values to the DataGridView just fine. I created a test-button in the test-case form to print the values bound to the DataGridView into a textual representation. This works perfectly and I see the values I have entered.
So here comes the weird part. When I retrieve an object WITHOUT any PersonCars (Count == 0) I can add as many PersonCars as I like (just what you would expect). However when I start printing the values bound to the DataGridView they don't match. All the objects in the PersonCars collection are uninitialized. They aren't NULL but the values are default values. So it looks like the DataGridView isn't updating the values back into the correct objects.
You can download the test-case here:
http://home.planet.nl/~lyckl022/temp/Em ... roblem.zipBelow are the steps you need to perform to see the result for your self:
STEP 1: Select the first Person in the left DataGridView (should already be selected when you open the form).
STEP 2: Select add as many PersonCars in the right DataGridView as you like.
STEP 3: Click the button in the right bottom corner to see the values in the right DataGridView being shown in a textual representation. The values should be the same as what you entered.
STEP 4: Select the second Person in the left DataGridView.
STEP 5: Select add as many PersonCars in the right DataGridView as you like.
STEP 6: Click the button in the right bottom corner to see the values in the right DataGridView being shown in a textual representation. As you now hopefully see, the values do NOT match with what you have entered. In fact the whole object looks like it has just been initialized and never bound correctly to the DataGridView.
There is a simple "fix" for this problem though:
Code:
public IList<PersonCar> Cars {
get {
if (this.cars.Count == 0)
this.cars = new List<PersonCar>();
return this.cars;
}
}
Just set the instance of the cars variable to a new List<PersonCar>() whenever you call the Cars property. However this way you won't be able to persist the data because the following error will occur if you do so:
Quote:
A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: <Type Of Class>
I'm running out of idea's... I really really hope someone can help me out!