The following example demonstrates how to bind to a data source that implements IQueryable (LINQ DataContext) and allow items to be edited, deleted, inserted, and refreshed.
The following code provides the code-behind implementation of the CommitMode, CreatingNewItem, CommittingNewItem, CancelingNewItem, CommitItems, and RemovingItem events.
PublicPartialClass Window1
Inherits Window
PublicSubNewMe.DataContext = Me
InitializeComponent()
End Sub' QUERYABLE SOURCE
PublicReadonlyProperty QueryableSource As IQueryable
GetIf m_queryable IsNothingThen
m_northwind = New NorthwindDataContext()
m_queryable = m_northwind.Products
EndIfReturn m_queryable
EndGetEnd PropertyPrivate m_northwind As NorthwindDataContext
Private m_queryable As IQueryable
' QUERYABLE INSERTION SUPPORT
PrivateSub DataGridVirtualizingQueryableCollectionViewSource_CreatingNewItem( sender AsObject, e As DataGridCreatingNewItemEventArgs )
Dim productToInsert AsNew Product()
e.NewItem = productToInsert
m_northwind.Products.InsertOnSubmit( productToInsert )
e.Handled = TrueEnd SubPrivateSub DataGridVirtualizingQueryableCollectionViewSource_CommittingNewItem( sender AsObject, e As DataGridCommittingNewItemEventArgs )
Try
m_northwind.SubmitChanges()
Catch e As Exception
e.Cancel = TrueEndTry
e.Handled = TrueEnd SubPrivateSub DataGridVirtualizingQueryableCollectionViewSource_CancelingNewItem( sender AsObject, e As DataGridItemHandledEventArgs )
m_northwind.GetChangeSet().Inserts.Clear()
e.Handled = TrueEnd Sub' QUERYABLE EDIT SUPPORT
PrivateSub DataGridVirtualizingQueryableCollectionViewSource_CommitItems( sender AsObject, e As CommitItemsEventArgs )
Try
m_northwind.SubmitChanges()
Catch e As Exception
m_northwind.GetChangeSet().Updates.Clear()
Finally
e.AsyncCommitInfo.EndCommit()
EndTryEnd Sub' QUERYABLE DELETE SUPPORT
PrivateSub DataGridVirtualizingQueryableCollectionViewSource_RemovingItem( sender AsObject, e As DataGridRemovingItemEventArgs )
Try
m_northwind.Products.DeleteOnSubmit( TryCast( e.Item, Product ) )
m_northwind.SubmitChanges()
Catch e As Exception
m_northwind.GetChangeSet().Deletes.Clear()
e.Cancel = TrueEndTry
e.Handled = TrueEnd SubEnd Class
The following code provides the code-behind implementation of the CommitMode, CreatingNewItem, CommittingNewItem, CancelingNewItem, CommitItems, and RemovingItem events.
Target Platforms: Windows 11, Windows 10, Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2