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 Source={x:Static Application.Current}, Path=LinqDataContext.Orders}"/> <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> <xcdg:CellEditor x:Key="employeeEditor"> <xcdg:CellEditor.EditTemplate> <DataTemplate> <ComboBox ItemsSource="{Binding Source={x:Static Application.Current}, Path=LinqDataContext.Employees}" SelectedItem="{xcdg:CellEditorBinding}"/> </DataTemplate> </xcdg:CellEditor.EditTemplate> </xcdg:CellEditor> <xcdg:CellEditor x:Key="customerEditor"> <xcdg:CellEditor.EditTemplate> <DataTemplate> <ComboBox ItemsSource="{Binding Source={x:Static Application.Current}, Path=LinqDataContext.Customers}" SelectedItem="{xcdg:CellEditorBinding}"/> </DataTemplate> </xcdg:CellEditor.EditTemplate> </xcdg:CellEditor> <xcdg:CellEditor x:Key="shipperEditor"> <xcdg:CellEditor.EditTemplate> <DataTemplate> <ComboBox ItemsSource="{Binding Source={x:Static Application.Current}, Path=LinqDataContext.Shippers}" SelectedItem="{xcdg:CellEditorBinding}"/> </DataTemplate> </xcdg:CellEditor.EditTemplate> </xcdg:CellEditor> </Grid.Resources> <DockPanel> <Button Content="Save Modifications" Click="SaveModifications" DockPanel.Dock="Top" /> <xcdg:DataGridControl x:Name="OrdersGrid" ItemsSource="{Binding Source={StaticResource cvs_orders}}"> <xcdg:DataGridControl.Columns> <xcdg:Column FieldName="OrderID" Visible="False"/> <xcdg:Column FieldName="EmployeeID" Visible="False"/> <xcdg:Column FieldName="Employee" CellEditor="{StaticResource employeeEditor}"/> <xcdg:Column FieldName="CustomerID" Visible="False"/> <xcdg:Column FieldName="Customer" CellEditor="{StaticResource customerEditor}" Title="Company Name"/> <xcdg:Column FieldName="ShipVia" Visible="False"/> <xcdg:Column FieldName="Shipper" CellEditor="{StaticResource shipperEditor}"/> </xcdg:DataGridControl.Columns> <xcdg:DataGridControl.View> <xcdg:TableView> <xcdg:TableView.FixedFooters> <DataTemplate> <xcdg:InsertionRow/> </DataTemplate> </xcdg:TableView.FixedFooters> </xcdg:TableView> </xcdg:DataGridControl.View> </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(); } |