Welcome to the Xceed Community | Help
Community Search  
More Search Options

Binding to a LINQ query (SQL)

Sort Posts: Previous Next
  •  10-08-2008, 3:58 PM Post no. 28130

    Binding to a LINQ query (SQL)

    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.

    <Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"

        <xcdg:DataGridCollectionViewSource x:Key="cvs_orders"
                                          Source="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}},
            <xcdg:DataGridGroupDescription PropertyName="Shipper"/>

        <DataTemplate DataType="{x:Type local:Shipper}">
          <TextBlock Text="{Binding CompanyName}"/>

        <DataTemplate DataType="{x:Type local:Customer}">
          <TextBlock Text="{Binding CompanyName}"/>

        <DataTemplate DataType="{x:Type local:Employee}">
          <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding FirstName}"/>
            <TextBlock Text=" " />
            <TextBlock Text="{Binding LastName}"/>

        <StackPanel Orientation="Horizontal"

          <Button Content="Save Modifications"
          <ComboBox x:Name="ShipperCombo"
                    ItemsSource="{Binding Source={x:Static Application.Current}, Path=LinqDataContext.Shippers}"

        <xcdg:DataGridControl x:Name="OrdersGrid"
                              ItemsSource="{Binding Source={StaticResource cvs_orders}}">
            <xcdg:Column FieldName="Shipper"
            <xcdg:Column FieldName="OrderID"
            <xcdg:Column FieldName="EmployeeID"
            <xcdg:Column FieldName="CustomerID"
            <xcdg:Column FieldName="Customer"
                         Title="Company Name"/>
            <xcdg:Column FieldName="ShipVia"

    The Window1 class implements INotifyPropertyChanged so that the DataGridCollectionViewSource can be notified
View as RSS news feed in XML
Contact | Site Map | Reviews | Legal Terms of Use | Trademarks | Privacy Statement Copyright 2011 Xceed Software Inc.