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. The following code provides the code-behind implementation of the CommitMode, CreatingNewItem, CommittingNewItem, CancelingNewItem, CommitItems, and RemovingItem events.
<Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid">
<Grid.Resources>
<xcdg:DataGridVirtualizingQueryableCollectionViewSource x:Key="cvs_queryableSource"
QueryableSource="{Binding Path=QueryableSource}"
CommitMode="EditCommitted"
CreatingNewItem="DataGridVirtualizingQueryableCollectionViewSource_CreatingNewItem"
CommittingNewItem="DataGridVirtualizingQueryableCollectionViewSource_CommittingNewItem"
CancelingNewItem="DataGridVirtualizingQueryableCollectionViewSource_CancelingNewItem"
CommitItems="DataGridVirtualizingQueryableCollectionViewSource_CommitItems"
RemovingItem="DataGridVirtualizingQueryableCollectionViewSource_RemovingItem" />
</Grid.Resources>
<xcdg:DataGridControl ItemsSource="{Binding Source={StaticResource cvs_queryableSource}}"
ItemScrollingBehavior="Deferred"
MaxGroupLevels="2"
MaxSortLevels="2"
IsDeleteCommandEnabled="True"
IsRefreshCommandEnabled="True">
<xcdg:DataGridControl.Resources>
<Style TargetType="{x:Type xcdg:Row}"
x:Key="RowHeightStyle">
<Setter Property="Height"
Value="27" />
</Style>
<Style TargetType="{x:Type xcdg:DataRow}"
BasedOn="{StaticResource RowHeightStyle}" />
<Style TargetType="{x:Type xcdg:InsertionRow}"
BasedOn="{StaticResource RowHeightStyle}" />
</xcdg:DataGridControl.Resources>
<xcdg:DataGridControl.View>
<xcdg:TableView>
<xcdg:TableView.FixedHeaders>
<DataTemplate>
<xcdg:InsertionRow />
</DataTemplate>
</xcdg:TableView.FixedHeaders>
</xcdg:TableView>
</xcdg:DataGridControl.View>
<xcdg:DataGridControl.Columns>
<xcdg:Column FieldName="ProductID"
AllowSort="False"
AllowGroup="False" />
</xcdg:DataGridControl.Columns>
</xcdg:DataGridControl>
</Grid>
Public Partial Class Window1
Inherits Window
Public Sub New
Me.DataContext = Me
InitializeComponent()
End Sub
' QUERYABLE SOURCE
Public Readonly Property QueryableSource As IQueryable
Get
If m_queryable Is Nothing Then
m_northwind = New NorthwindDataContext()
m_queryable = m_northwind.Products
End If
Return m_queryable
End Get
End Property
Private m_northwind As NorthwindDataContext
Private m_queryable As IQueryable
' QUERYABLE INSERTION SUPPORT
Private Sub DataGridVirtualizingQueryableCollectionViewSource_CreatingNewItem( sender As Object, e As DataGridCreatingNewItemEventArgs )
Dim productToInsert As New Product()
e.NewItem = productToInsert
m_northwind.Products.InsertOnSubmit( productToInsert )
e.Handled = True
End Sub
Private Sub DataGridVirtualizingQueryableCollectionViewSource_CommittingNewItem( sender As Object, e As DataGridCommittingNewItemEventArgs )
Try
m_northwind.SubmitChanges()
Catch e As Exception
e.Cancel = True
End Try
e.Handled = True
End Sub
Private Sub DataGridVirtualizingQueryableCollectionViewSource_CancelingNewItem( sender As Object, e As DataGridItemHandledEventArgs )
m_northwind.GetChangeSet().Inserts.Clear()
e.Handled = True
End Sub
' QUERYABLE EDIT SUPPORT
Private Sub DataGridVirtualizingQueryableCollectionViewSource_CommitItems( sender As Object, e As CommitItemsEventArgs )
Try
m_northwind.SubmitChanges()
Catch e As Exception
m_northwind.GetChangeSet().Updates.Clear()
Finally
e.AsyncCommitInfo.EndCommit()
End Try
End Sub
' QUERYABLE DELETE SUPPORT
Private Sub DataGridVirtualizingQueryableCollectionViewSource_RemovingItem( sender As Object, 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 = True
End Try
e.Handled = True
End Sub
End Class
public partial class Window1 : Window
{
public Window1()
{
this.DataContext = this;
InitializeComponent();
}
// QUERYABLE SOURCE
public IQueryable QueryableSource
{
get
{
if( m_queryable == null )
{
m_northwind = new NorthwindDataContext();
m_queryable = m_northwind.Products;
}
return m_queryable;
}
}
private NorthwindDataContext m_northwind;
private IQueryable m_queryable;
// QUERYABLE INSERTION SUPPORT
private void DataGridVirtualizingQueryableCollectionViewSource_CreatingNewItem( object sender, DataGridCreatingNewItemEventArgs e )
{
Product productToInsert = new Product();
e.NewItem = productToInsert;
m_northwind.Products.InsertOnSubmit( productToInsert );
e.Handled = true;
}
private void DataGridVirtualizingQueryableCollectionViewSource_CommittingNewItem( object sender, DataGridCommittingNewItemEventArgs e )
{
try
{
m_northwind.SubmitChanges();
}
catch
{
e.Cancel = true;
}
e.Handled = true;
}
private void DataGridVirtualizingQueryableCollectionViewSource_CancelingNewItem( object sender, DataGridItemHandledEventArgs e )
{
m_northwind.GetChangeSet().Inserts.Clear();
e.Handled = true;
}
// QUERYABLE EDIT SUPPORT
private void DataGridVirtualizingQueryableCollectionViewSource_CommitItems( object sender, CommitItemsEventArgs e )
{
try
{
m_northwind.SubmitChanges();
}
catch
{
m_northwind.GetChangeSet().Updates.Clear();
}
finally
{
e.AsyncCommitInfo.EndCommit();
}
}
// QUERYABLE DELETE SUPPORT
private void DataGridVirtualizingQueryableCollectionViewSource_RemovingItem( object sender, DataGridRemovingItemEventArgs e )
{
try
{
m_northwind.Products.DeleteOnSubmit( e.Item as Product );
m_northwind.SubmitChanges();
}
catch
{
m_northwind.GetChangeSet().Deletes.Clear();
e.Cancel = true;
}
e.Handled = true;
}
}
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