DataGridView and IEditableObject_CancelEdit

DataGridView and IEditableObject_CancelEdit

Old forum URL: forums.lhotka.net/forums/t/2124.aspx


JZuerlein posted on Thursday, January 11, 2007

I'm stuck on a problem that I'm sure someone has run into before, so I'm looking for advice.

I have a parent object with a collection of children.  The parent gets bound to a winform, and the child collection gets bound to a DataGridView.  I can add items and save fine.  The problem comes into play when I ....

1)  Add a row to the DGV(DataGridView) using the DGV functionality, and enter valid data.

2) Add a second row to the DGV, and I do not do any data entry.

3) Click on a cell in the first row I added.

The DGV will remove the second row that I added.  Clear the values from the first row that I added, and the bound object will no longer have the data from the data entry.

Why is this?  I have no validation checks on the business objects.  The only thing I can think of is that the field I use as a unique identifier for the object doesn't get assigned until it is persisted to the database. (Identity column in the table.)  Could the DGV be getting confused about which row to remove when the cancel edit occurs?

 

ajj3085 replied on Thursday, January 11, 2007

JZuerlein:
(Identity column in the table.)  Could the DGV be getting confused about which row to remove when the cancel edit occurs?


That's exactly the case.  The best solution is to create a private static int field starting at 0 or -1, and on every new object creation, decrement that value.  Check out the FAQ for more detail.

Copyright (c) Marimer LLC