Delete via sorted datagridview removes wrong row

Posts   
 
    
Markiemac
User
Posts: 132
Joined: 25-Apr-2006
# Posted on: 21-Apr-2007 15:22:08   

Hi,

I'm using latest V2, Adapter & VB2005. When I delete a row via my datagridview, I use adapter.DeleteEntity(myEntity) to delete it from the database, and if successfull I then remove the row from the grid.

This works ok as long as the grid is not sorted. If I click on a column to sort my rows then select a row for deletion, the selected row is deleted from the db, but a different row is removed from my grid.

It seems that when I remove the row using:

CType(BindingSource1.DataSource(), EntityCollection(Of EntityClasses.EmployeeEntity)).RemoveAt(rowNum)

rowNum of the grid is no longer the same as the index of the EntityCollection because the grid has been sorted, hence the problem.

a) Does anyone have an easy work-round for this?

or

b) Using a PK, how can I get the index of an item from the EntityCollection, so that I remove the correct item from the datagridview.

Thanks simple_smile

Posts: 254
Joined: 16-Nov-2006
# Posted on: 21-Apr-2007 22:54:56   

Hi Mark

Few questions

1) Can you pleaes provide more code here e.g. showing the code which removes the entity? 2) How is the entity retrieved to delete it using DeleteEntity? 3) Presumably from 2) your not using data binding with the grid if your removing it explicitly, why aren't you using data binding in your scenario?

If you are using data binding then you can certainly use the UserDeletedRow event handlers to obtain through the event arguments, a DataGridViewRow from which you can obtain the Index of the row in the grid, and also the entity from the DataBoundItem property.

Markiemac
User
Posts: 132
Joined: 25-Apr-2006
# Posted on: 22-Apr-2007 17:02:28   

Hi Matt, The entity name is different, but the problem is the same.

3) Presumably from 2) your not using data binding with the grid if your removing it explicitly, why aren't you using data binding in your scenario?

I am using data binding. The datagridview is bound to an EntityCollection via a BindingSource for design time layout of the columns in the grid. On formLoad I retrieve all the rows for the grid as per

    Function GetAllWorldBankDevClass() As EntityCollection(Of WorldBankDevClassEntity)
        Dim adapter As New DataAccessAdapter
        Try
            Dim allWorldBankDevClass As New EntityCollection(Of WorldBankDevClassEntity)(New WorldBankDevClassEntityFactory())
            adapter.FetchEntityCollection(allWorldBankDevClass, Nothing)
            allWorldBankDevClass.AllowRemove = True
            Return allWorldBankDevClass
        Finally
            adapter.Dispose()
        End Try
    End Function

2) How is the entity retrieved to delete it using DeleteEntity?

User selects row, then the Delete button or Delete key (I use UserDeletedRow) to perform:


    Private Sub DeleteRow()
        'Used by both btnDelete control and Delete key
        Dim rowNum As Integer
        Dim rowID As String
        Dim rowName As String
        Dim wasDeleted As Integer
        rowNum = Me.DataGridView1.CurrentRow.Index
        rowID = Me.DataGridView1.CurrentRow.Cells(0).Value.ToString
        rowName = CStr(Me.DataGridView1.CurrentRow.Cells(1).Value)
**Snip**
        Dim wbDevClass As New LookupTableManager
        wasDeleted = wbDevClass.DeleteWorldBankDevClass(rowID)
        Select Case wasDeleted
            Case 0  'Deleted ok
                CType(BindingSource1.DataSource(), EntityCollection(Of EntityClasses.WorldBankDevClassEntity)).RemoveAt(rowNum)
**snip**

1) Can you please provide more code here e.g. showing the code which removes the entity?

LookupTableManager performs following code , if its safe to delete.

    Public Shared Function EntityDeleted(ByVal myEntity As IEntity2) As Boolean
        'Pass in the entity with its PK value to return True or False
        Dim adapter As New DataAccessAdapter()
        Try
            Return adapter.DeleteEntity(myEntity)
        Finally
            adapter.Dispose()
        End Try
    End Function

The problem only manifests itself when the grid is sorted. In Private Sub DeleteRow (above) I use

        rowNum = Me.DataGridView1.CurrentRow.Index
        rowID = Me.DataGridView1.CurrentRow.Cells(0).Value.ToString

to identify the row to be removed from the grid (rowNum) and the PK (rowID) for the entity to be deleted from the collection. However, after the entity has been deleted, the row removed from the grid is a row that was originally at that position, before the grid was sorted. It's as if the grid is reloaded, after the entity is deleted from the collection and before I remove the row. confused .

Any ideas?

<EDIT> I wonder if this post (Delete multiple records from a sorted databound DataGridview) in March is another aspect of the same problem?

Thanks

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 23-Apr-2007 07:47:39   

Hi Mark. If you are using _UserDeletedRow _event you can use e.Row to access the row that its being deleted. In this event you should delete from DB or add the entity to a entitiesToDelete entityCollection. Is no clear to me why are you deleting from the entitycollection as the deleteRow deletes the entity for the underlying datasource (your collection).

David Elizondo | LLBLGen Support Team
Markiemac
User
Posts: 132
Joined: 25-Apr-2006
# Posted on: 23-Apr-2007 13:42:42   

Hi daelmo,

I can see that e.Row gives me the same row as

        rowNum = Me.DataGridView1.CurrentRow.Index

so no problem there.

Is no clear to me why are you deleting from the entitycollection as the deleteRow deletes the entity for the underlying datasource (your collection).

Ok, just realised that I was deleting from my collection AND the DB. Removing the deleted row directly from the grid has solved the problem.

I am a little confused from reading the documentation as to whether 'DeleteEntity(myEntity)' actually deletes directly from the DB. I know that 'DeleteEntitiesDirectly' does do that, but it does not return a result to tell me if the delete was successful.

Does 'DeleteEntity(myEntity)' actually delete directly from the DB as well?

Thanks

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 24-Apr-2007 04:25:18   

Does 'DeleteEntity(myEntity)' actually delete directly from the DB as well?

_Copy from the LLBLGenPro Reference Manual:_:)

Syntax:

virtual bool DeleteEntity( 
   IEntity2 entityToDelete)

Overview: Deletes the specified entity from the persistent storage. The entity is not usable after this call, the state is set to OutOfSync. Will use the current transaction if a transaction is in progress.

Return Value true if the delete was succesful, otherwise false.

David Elizondo | LLBLGen Support Team