In This Topic
    Binding to a LINQ table
    In This Topic

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

    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 LinqDataContext that returns a new instance of the NorthwindDataContext exists in the App.xaml.cs code-behind file.

    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
    Private Sub SaveModifications( sender As Object, e As RoutedEventArgs )
       App.LinqDataContext.SubmitChanges()
    End Sub
    C#
    Copy Code
    private void SaveModifications( object sender, RoutedEventArgs e )
    {    
     App.LinqDataContext.SubmitChanges();
    }