Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
2265
Grid bound to Entity Framework and Row Template for New Row
posted

Hi,

I am binding my grid to some Entity Framework objects.  I am getting spurious new entities appearing.

I am using the System.Windows.Forms.BindingSource to set the top level object to.  Then I have configured the UltraGrid in the designer to use the BindingSource instance as the grids datasource.

I have 3 bands.  For example:

ClassA (Band 0)
     List   (Band 1) - hidden
     List   (Band 2)

So ClassA has list references to ClassB and ClassC.   I am showing Band 0 and 2.  1 is hidden.

I am using FixedAddRowOnTop to add rows to both Band 0 and Band 2.  When I click on the new row in Band 0, the AddingNew event is fired on the binding source.  If edit a row in Band 0 then click off, the AfterRowUpdate event is fired where I can then add the row to my entities context.

If I do the same for Band 2 then 2 new rows are added to my entities context. Every time I click on the row template header, a new instance of ClassB is created and added automatically to my entity context.

Is there a way to resolve this so that the new instance is not added or I can get a chance to remove it?

I have to work around this just before I do the save:

Private Sub RemoveDodgyEntities()
        For Each added In _context.ObjectStateManager.GetObjectStateEntries(EntityState.Added)
            Dim found As Boolean = False
            ' found
            For Each p In MYGRID.Rows
                For Each r In p.ChildBands(0).Rows
                    If r.ListObject IsNot Nothing Then
                        If Object.ReferenceEquals(r.ListObject, added.Entity) Then
                            found = True
                            Exit For
                        End If
                        If found Then Exit For
                        'Diagnostics.Trace.WriteLine(r.ListObject.ToString())

                    End If
                Next
            Next
            If Not found Then
                _context.DeleteItem(added.Entity)
            End If
        Next

    End Sub

 

Thanks

Andez

 

 

  • 53790
    posted

    Hello Andrez,

    Maybe one possible solution could be if you call your SaveChanges() method later. You could create instance (one or more) of your entity object and  add these object to your enity. By this way you will be able to see the  objects like a rows into your grid. You could delete it or modify it, before to decide to save these change. Please take a look at the attached video file for more details and let me know if you have any questions.

    Regards

    Video030.rar
  • 71886
    Offline posted

    Hello Andez,

    Just to clarify - what you want is to not commit the row to the datasource on click with the mouse in a cell?