Is it ok to add the same Entity to multiple EntityCollection for binding to different grid? (EntityBase2.ParentCollection)

Posts   
 
    
Max avatar
Max
User
Posts: 221
Joined: 14-Jul-2006
# Posted on: 27-Jan-2009 11:14:06   

I have a winForm with two grids (Infragistics UltraGrid), the top grid is used for data listing/filtering and is read-only, the bottom grid is used for editing the row selected in the top grid.(the two grids have very different layout)

My idea is to fetch an entityCollection with the data to bind to the top grid; then get the entity selected in the top grid, add this entity to a new entityCollection (that will contain only this entity) and bind this new entityCollection to the bottom grid for entity-editing.

So, I will have a single entinty (the same instance) added to two different entityCollection, bound to two different grid (one read only, the other one for editing)

Will it work?

I saw that the EntityBase2 have a ParentCollection property that can point to a single collection. Will this give me problem? Why the EntityBase2.ParentCollection is used only by the EntityCollection2.AddNew and is not used by EntityCollection2.Add? I see in the comments that EntityBase2.ParentCollection is data-binding related... but I can't find where it's used.

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 27-Jan-2009 11:36:07   

Instead of adding the selected entity to another collection, use the same fetched collection in both of the grids.

Max avatar
Max
User
Posts: 221
Joined: 14-Jul-2006
# Posted on: 27-Jan-2009 12:10:41   

Walaa wrote:

Instead of adding the selected entity to another collection, use the same fetched collection in both of the grids.

Mmmh... it's not so easy.

In the top grid I'll have 10 or 100 entity, but in the bottom grid I need to show only one single entity, so I can't directly use the same entityCollection. I can use filtered entityView, this will work for fetched entity (because I can create an EntityView filtered by the key of the selected entity), but it's harder to manage for newly created entity that doesn't have key column populated. For newely created entity I would like to be able to recursively save (and refetch) those entity (and any eventual sub-entity), and only then add those entities to the entityCollection of the top grid.

I will need to do a recursive save, because I'll have sub entity. If I'll use a single entityCollection, i believe (but I'm not sure) that doing a recursive save on the newly created entity will save the whole entityCollection bound to the top grid, and I don't want to save the whole entityCollection, because I've only edited and checked/validated the single (eventually newly created) entity + related subEntities.

I'm trying to maintain a clear separation between the data that is going to be edited/validated/saved, and the data that will only be showed in the top grid.

Thanks, Max

Max avatar
Max
User
Posts: 221
Joined: 14-Jul-2006
# Posted on: 27-Jan-2009 15:40:20   

After further investigation, I've found that the EntityBase2.ParentCollection is used in the .CancelEdit to remove the entity from the collection when the entity has been added using entityCollection.AddNew (maybe this is the way databinding add a new item to an existing collection...)

In my situation this shouldn't matter, because I never let the databinding add new entities; I always add new entity via explicit code, using EntityCollection.Add

But I didn't liked the idea of doing something that might not work... So, following the Walaa's suggestion, I've used a single entityCollection. simple_smile

The top grid is bound to the entityCollection. The bottom grid is bound to an entitiView of the entityCollection, this entityView is filtered using a DelegatePredicate that show only the entity instance selected in the upper grid.

code of the entityView

evFascicolo = New EntityView2(Of FascicoloEntity)(Me.bcFascicoli.EntityCollectionGen)
evFascicolo.Filter = New DelegatePredicate(Of FascicoloEntity)(Function(lFascicolo As FascicoloEntity) lFascicolo Is entityFilteredInDetail)

Code for the AfterRowActivate event of the top grid

Private Sub gcLista_AfterRowActivate(ByVal sender As Object, ByVal e As System.EventArgs) Handles gcLista.AfterRowActivate
    Try

        'get the Entity selected in the upper grid
        Dim ent As Object
        ent = Me.gcLista.UltraGrid.ActiveRow.ListObject
        
        'set the selecten entity in the parameter used by the entityViewFilter
        Me.entityFilteredInDetail = DirectCast(ent, FascicoloEntity)

        'force the entityView to refresh itself
        Me.evFascicolo.Filter = Me.evFascicolo.Filter

    Catch ex As Exception
        Log(ex)
    End Try
End Sub