The following example demonstrates how to bind a grid to an SQL LINQ query and submit any modifications made to the data items using the SubmitChanges method. 

Although existing data items can be modified and the changes committed, it is not possible to insert new data items.

This example assumes that a new LINQ to SQL Classes item named Northwind.dbml has been added to the project and that it contains the Orders, Customers, and Shippers tables. The Northwind.designer.cs that is created at the same time represents the NorthwindDataContext and should automatically contain all the relevant members. It also assumes that a property named OrdersQuery that returns a new new query based on the value selected in the combo box.

The Window1 class implements INotifyPropertyChanged so that the DataGridCollectionViewSource can be notified when the query changes in order to refresh its content.

For more information on LINQ, refer to The LINQ Project Web site.

XAML
Copy Code
<Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"
      xmlns:local="clr-namespace:Xceed.Wpf.Documentation">
  <Grid.Resources>
    <xcdg:DataGridCollectionViewSource x:Key="cvs_orders"
                                      Source="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}},
                                                       Path=OrdersQuery}">
      <xcdg:DataGridCollectionViewSource.GroupDescriptions>
        <xcdg:DataGridGroupDescription PropertyName="Shipper"/>
      </xcdg:DataGridCollectionViewSource.GroupDescriptions>
    </xcdg:DataGridCollectionViewSource>
    <DataTemplate DataType="{x:Type local:Shipper}">
      <TextBlock Text="{Binding CompanyName}"/>
    </DataTemplate>
    <DataTemplate DataType="{x:Type local:Customer}">
      <TextBlock Text="{Binding CompanyName}"/>
    </DataTemplate>
    <DataTemplate DataType="{x:Type local:Employee}">
      <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding FirstName}"/>
        <TextBlock Text=" " />
        <TextBlock Text="{Binding LastName}"/>
      </StackPanel>
    </DataTemplate>       
  </Grid.Resources>
  <DockPanel>
    <StackPanel Orientation="Horizontal"
                DockPanel.Dock="Top">
      <Button Content="Save Modifications"
              Click="SaveModifications"/>
      <ComboBox x:Name="ShipperCombo"
                ItemsSource="{Binding Source={x:Static Application.Current}, Path=LinqDataContext.Shippers}"
                SelectionChanged="ShipperSelectionChanged"/>
    </StackPanel>
    <xcdg:DataGridControl x:Name="OrdersGrid"
                          ItemsSource="{Binding Source={StaticResource cvs_orders}}">
      <xcdg:DataGridControl.Columns>
        <xcdg:Column FieldName="Shipper"
                     VisiblePosition="0"/>
        <xcdg:Column FieldName="OrderID"
                     Visible="False"/>
        <xcdg:Column FieldName="EmployeeID"
                     Visible="False"/>
        <xcdg:Column FieldName="CustomerID"
                     Visible="False"/>
        <xcdg:Column FieldName="Customer"
                     Title="Company Name"/>
        <xcdg:Column FieldName="ShipVia"
                     Visible="False"/>
      </xcdg:DataGridControl.Columns>
    </xcdg:DataGridControl>
  </DockPanel>
</Grid>
VB.NET
Copy Code
Namespace Xceed.Wpf.Documentation
Public Partial Class Window1
    Inherits Window
    Implements INotifyPropertyChanged
    Public Sub New()
      InitializeComponent()
    End Sub
    Private m_query As IEnumerable = Nothing
    Public Property OrdersQuery() As IEnumerable
      Get
        If m_query Is Nothing Then
          m_query = From orders In App.LinqDataContext.Orders _
                    Select orders
        End If
        Return m_query
      End Get
      Set
        m_query = Value
        Me.OnPropertyChanged(New PropertyChangedEventArgs("OrdersQuery"))
      End Set
    End Property
    Private Sub ShipperSelectionChanged(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)
      Me.OrdersQuey = From orders In App.LinqDataContext.Orders _
                 Where orders.Shipper.CompanyName = CTYpe( Me.ShipperCombo.SelectedValue, Shipper).CompanyName _
                 Select orders
    End Sub
    Private Sub SaveModifications(ByVal sender As Object, ByVal e As RoutedEventArgs)
      App.LinqDataContext.SubmitChanges()
    End Sub
    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
    Private Sub OnPropertyChanged(ByVal e As PropertyChangedEventArgs)
      If Me.PropertyChangedEvent Is Nothing Then
        Return
      End If
      RaiseEvent PropertyChanged(Me, e)
    End Sub
  End Class
End Namespace
C#
Copy Code
namespace Xceed.Wpf.Documentation
{
 public partial class Window1 : Window, INotifyPropertyChanged
 {
   public Window1()
   {
     InitializeComponent();    
   }
   private IEnumerable m_query = null;
   public IEnumerable OrdersQuery
   {
     get
     {
       if( m_query == null )
       {
         m_query = from orders in App.LinqDataContext.Orders
                   select orders;
       }
       return m_query;
     }
     set
     {
       m_query = value;
       this.OnPropertyChanged( new PropertyChangedEventArgs( "OrdersQuery" ) );
     }
   }
   private void ShipperSelectionChanged( object sender, SelectionChangedEventArgs e )
   {
     this.OrdersQuery = from orders in App.LinqDataContext.Orders
                 where orders.Shipper.CompanyName == ( ( Shipper )this.ShipperCombo.SelectedValue ).CompanyName
                 select orders;
   }
   private void SaveModifications( object sender, RoutedEventArgs e )
   {    
     App.LinqDataContext.SubmitChanges();
   }
   public event PropertyChangedEventHandler PropertyChanged;
   private void OnPropertyChanged( PropertyChangedEventArgs e )
   {
     if( this.PropertyChanged == null )
       return;
     this.PropertyChanged( this, e );
   }
 }
}