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.
.
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