KB Article A1630
Visible to All Users

Why are objects not deleted when I delete them in the XtraGrid?

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 Basic
Imports 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 Basic
Imports 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

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.