Description:
I have a DataNavigator and a DataGrid to show the content of an XPCollection and control it. I can insert a new item. But I can't delete it using the DataNavigator: I click the [-] button and the object disappear but next time I load the application the deleted objects are shown again. Why are these objects not deleted?
Answer:
When you click the [-] button the object is removed from the collection, but it's not deleted from the database. If you use XPO 6.2 or higher, please set the DeleteObjectOnRemove property of your XPCollection to True. If you use XPO 6.1, please add the following code to the handler of the collection's CollectionChanged event to delete the record:
C#using DevExpress.Xpo;
private void xpCollection1_CollectionChanged(object sender, XPCollectionChangedEventArgs e) {
if(e.CollectionChangedType == XPCollectionChangedType.AfterRemove)
(sender as XPCollection).Session.Delete(e.ChangedObject);
}
Visual BasicImports DevExpress.Xpo
Private Sub xpCollection1_CollectionChanged(ByVal sender As Object, ByVal e As _
XPCollectionChangedEventArgs) Handles XpCollection1.CollectionChanged
If e.CollectionChangedType = XPCollectionChangedType.AfterRemove Then
CType(sender, XPCollection).Session.Delete(e.ChangedObject)
End If
End Sub
When rows are deleted via the EmbeddedNavigator of your XtraGrid, you can utilize another solution: handle the ButtonClick event of the grid's EmbeddedNavigator. Here is the necessary code:
C#using DevExpress.Xpo;
using DevExpress.XtraGrid;
using DevExpress.XtraEditors;
private void gridControl1_EmbeddedNavigator_ButtonClick(object sender, NavigatorButtonClickEventArgs e) {
if(e.Button.ButtonType == DevExpress.XtraEditors.NavigatorButtonType.Remove) {
GridControl grid = ((ControlNavigator)sender).NavigatableControl as GridControl;
BindingManagerBase bm = grid.BindingContext[grid.FocusedView.DataSource];
IXPSimpleObject xpo = bm.Current as IXPSimpleObject;
if(xpo != null) {
xpo.Session.Delete(xpo);
e.Handled = true;
}
}
}
Visual BasicImports DevExpress.Xpo
Imports DevExpress.XtraEditors
Imports DevExpress.XtraGrid
Private Sub gridControl1_EmbeddedNavigator_ButtonClick(ByVal sender As Object, ByVal e As NavigatorButtonClickEventArgs)
If e.Button.ButtonType = DevExpress.XtraEditors.NavigatorButtonType.Remove Then
Dim grid As GridControl = CType(sender, ControlNavigator).NavigatableControl
Dim bm As BindingManagerBase = grid.BindingContext(grid.FocusedView.DataSource)
Dim xpo As Object = bm.Current
If Not xpo Is Nothing Then
xpo.Session.Delete(xpo)
e.Handled = True
End If
End If
End Sub
Note Never use the first and the second approach with the detail collections. The Session also removes items from the detail collection when the item's parent is changed. At this point, you should not delete the removed item from the database, but there is no way to determine whether the item was removed by the Session or by the external code.
The second solution will not work if you explicitly call the GridView.DeleteRow method, because in this case your code for the EmbeddedNavigator.ButtonClick event is not executed.
See Also: A147