I am really confused with how to DataBind. I have 2 nearly identical projects, one using LINQ, the other OpenAccess, using only the Categories table from the Northwind database. Basically, a grid (of that table), and 2 textboxes for editing / 2-way binding.
Here is the code for Window1.xaml (LINQ Project):
and from the OA project:
They should be identical (other than x:Class=...)
The Window1.xaml.cs files are the same (except for the namespace:
The viewmodels are as close as can be, based on different requirements for LINQ and OA:
I won't paste the code for the LINQ designer or the OA designer. Basically, just use the Categories table. From Northwind.
On the grid, moving up and down, the textboxes are updated in both apps.
The problem: If I edit in the grid, the changes are reflected in the textbox for the LINQ program and NOT the OA program.
What am I missing?
A follow up question... what is the best way to save the changes back to the database, and when? When the user moves off the record? When a button is pressed to confirm?
Here is the code for Window1.xaml (LINQ Project):
<Window x:Class="NWLinq.Window1" |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
xmlns:local="clr-namespace:NWLinq" |
Title="Northwind LINQ" Height="400" Width="575" xmlns:my="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView"> |
<Window.Resources> |
<ObjectDataProvider x:Key="objectDataProvider" ObjectType="{x:Type local:TestViewModel}" /> |
</Window.Resources> |
<Grid DataContext="{Binding Source={StaticResource objectDataProvider}}"> |
<Grid.ColumnDefinitions> |
<ColumnDefinition Width="200*" /> |
<ColumnDefinition Width="300*" /> |
</Grid.ColumnDefinitions> |
<Grid.RowDefinitions> |
<RowDefinition Height="60*" /> |
<RowDefinition Height="60*" /> |
<RowDefinition Height="240*" /> |
</Grid.RowDefinitions> |
<my:RadGridView |
Grid.Row="2" |
Name="radGridView1" |
Grid.ColumnSpan="2" |
ColumnsWidthMode="Fill" |
IsReadOnly="False" |
SelectedItem="{Binding SelectedItem}" |
ItemsSource="{Binding CategoryList}" |
> |
</my:RadGridView> |
<TextBlock Margin="91,16,41,10" Name="textBlock1" Text="Name" HorizontalAlignment="Right" VerticalAlignment="Center" /> |
<TextBlock Margin="91,14,41,8" Name="textBlock2" Grid.Row="1" Text="Description" HorizontalAlignment="Right" VerticalAlignment="Center" /> |
<TextBox Grid.Column="1" Margin="58,15,64,11" Name="textBox1" Text="{Binding Path=SelectedItem.CategoryName, Mode=TwoWay}" /> |
<TextBox Margin="58,14,64,8" Name="textBox2" Grid.Column="1" Grid.Row="1" Text="{Binding Path=SelectedItem.Description, Mode=TwoWay}" /> |
</Grid> |
</Window> |
and from the OA project:
<Window x:Class="NWOA.Window1" |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
xmlns:my="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView" |
xmlns:local="clr-namespace:NWOA" |
Title="Northwind OpenAccess" Height="400" Width="575" > |
<Window.Resources> |
<ObjectDataProvider x:Key="objectDataProvider" ObjectType="{x:Type local:TestViewModel}" /> |
</Window.Resources> |
<Grid DataContext="{Binding Source={StaticResource objectDataProvider}}"> |
<Grid.ColumnDefinitions> |
<ColumnDefinition Width="200*" /> |
<ColumnDefinition Width="300*" /> |
</Grid.ColumnDefinitions> |
<Grid.RowDefinitions> |
<RowDefinition Height="60*" /> |
<RowDefinition Height="60*" /> |
<RowDefinition Height="240*" /> |
</Grid.RowDefinitions> |
<my:RadGridView |
Grid.Row="2" |
Name="radGridView1" |
Grid.ColumnSpan="2" |
ColumnsWidthMode="Fill" |
IsReadOnly="False" |
SelectedItem="{Binding SelectedItem}" |
ItemsSource="{Binding CategoryList}" |
> |
</my:RadGridView> |
<TextBlock Margin="91,16,41,10" Name="textBlock1" Text="Name" HorizontalAlignment="Right" VerticalAlignment="Center" /> |
<TextBlock Margin="91,14,41,8" Name="textBlock2" Grid.Row="1" Text="Description" HorizontalAlignment="Right" VerticalAlignment="Center" /> |
<TextBox Grid.Column="1" Margin="58,15,64,11" Name="textBox1" Text="{Binding Path=SelectedItem.CategoryName, Mode=TwoWay}" /> |
<TextBox Margin="58,14,64,8" Name="textBox2" Grid.Column="1" Grid.Row="1" Text="{Binding Path=SelectedItem.Description, Mode=TwoWay}" /> |
</Grid> |
</Window> |
They should be identical (other than x:Class=...)
The Window1.xaml.cs files are the same (except for the namespace:
using System.Windows; |
namespace NWLinq |
{ |
public partial class Window1 : Window |
{ |
public Window1() |
{ |
InitializeComponent(); |
} |
} |
} |
------- |
using System.Windows; |
namespace NWOA |
{ |
public partial class Window1 : Window |
{ |
public Window1() |
{ |
InitializeComponent(); |
} |
} |
} |
The viewmodels are as close as can be, based on different requirements for LINQ and OA:
using System.Collections.Generic; |
using System.Linq; |
namespace NWLinq |
{ |
public class TestViewModel |
{ |
NWLinqDataContext dc = new NWLinqDataContext(); |
public TestViewModel() |
{ |
SetTable(); |
} |
public void SetTable() |
{ |
var query = |
from c in dc.Categories select c; |
CategoryList = query.ToList(); ; |
} |
public IList<Category> CategoryList |
{ |
get; |
set; |
} |
public Category SelectedItem |
{ |
get; |
set; |
} |
} |
} |
using System.Collections.Generic; |
using NWOpenAccess; |
using Telerik.OpenAccess; |
using Telerik.OpenAccess.Query; |
namespace NWOA |
{ |
public class TestViewModel |
{ |
private IObjectScope scope = NWScopeProvider.GetNewObjectScope(); |
public TestViewModel() |
{ |
SetTable(); |
} |
public void SetTable() |
{ |
var query = |
from c in this.scope.Extent<Category>() select c; |
CategoryList = query.ToList(); ; |
} |
public IList<Category> CategoryList |
{ |
get; |
set; |
} |
public Category SelectedItem |
{ |
get; |
set; |
} |
} |
} |
I won't paste the code for the LINQ designer or the OA designer. Basically, just use the Categories table. From Northwind.
On the grid, moving up and down, the textboxes are updated in both apps.
The problem: If I edit in the grid, the changes are reflected in the textbox for the LINQ program and NOT the OA program.
What am I missing?
A follow up question... what is the best way to save the changes back to the database, and when? When the user moves off the record? When a button is pressed to confirm?