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"
          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>

    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.