Object Collections bound to DataGridViews

Object Collections bound to DataGridViews

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


Crestline posted on Thursday, October 12, 2006

Hi,

We notice some strange behavior when a child collection object is bound to a datagridview.  On the child datagridview, we enter a couple new rows and then place focus on the next new row, just focus on it but don't actually enter anything,  If we then select a different  root item in the root collection datagridview and go bacjk to the original one where we edited the child collection, we are missing the first new child we added and it actually loaded the blank child to the end.

Click on Root1
    child1
    child2
    newchild1
    newchild2
    blankchild

Click on Root2

Go back to Root1
    child1
    child2
    newchild2
    blankchild

It's like the new ones were shifted, dropping the first new one and adding in the blank one.

Any one else run across this?

Thanks.

Brian Criswell replied on Thursday, October 12, 2006

It sounds like you are using identity fields in your database and you have the GetIdValue() method return the uninitialized id for the new objects.  All of your new objects have an id of 0 (or whatever your default value is).  When you do a cancel edit in the DataGridView it tells the underlying list to remove the item.  The list determines the item by calling object.Equals(object) on each item.  Since the Equals method returns the same value for every new item, the first new item is removed from the list.  Do a search for discussions on thread safe default values for integers and you should come across a solution for this.  The thread was about a month ago.

Crestline replied on Thursday, October 12, 2006

Thanks Brian.

We don't use GUIDs at this time so your right, we are using database identity fields.  We'll take a look at this and see what's happening.  In the past, in other non CSLA projects, we have set the id of a dataset's datatable to auto increment by -1 so that the in memory items and those read from the db never collide..



Copyright (c) Marimer LLC