P
piedatt80
Guest
In MVVM Light and Entity Framework I have written the following code that, quite simply, it allows me to display a list of customers within a Datagrid. My problem is that I cant, once you have changed the order of the rows by moving them or at the top or bottom, to save these changes. I initially decided to use the ObservableCollection Move, but unfortunately I can not find a good solution.
MyContext:
public partial class Customer
{
public int ID { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
}
public class MyContext : DbContext
{
public MyContext()
: base("name=MyContext")
{
Database.SetInitializer<MyContext>(new CreateDatabaseIfNotExists<MyContext>());
}
public DbSet<Customer> Customers { get; set; }
}
XAML:
<Grid x:Name="LayoutRoot">
<DataGrid ItemsSource="{Binding CustomerOC, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding SelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="False" SelectionUnit="FullRow" SelectionMode="Single" IsReadOnly="True" HorizontalAlignment="Left" Margin="10,45,0,0" VerticalAlignment="Top" Height="214" Width="200">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding ID}" />
<DataGridTextColumn Header="FirstName" Binding="{Binding FirstName}" />
<DataGridTextColumn Header="LastName" Binding="{Binding LastName}" Width="*" />
</DataGrid.Columns>
</DataGrid>
<Button Content="Select" Command="{Binding SearchCommand}" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75"/>
<Button Content="Save" Command="{Binding SaveCommand}" HorizontalAlignment="Left" Margin="90,10,0,0" VerticalAlignment="Top" Width="75"/>
<Button Content="Up" Command="{Binding UpCommand}" HorizontalAlignment="Left" Margin="215,48,0,0" VerticalAlignment="Top" Width="37"/>
<Button Content="Down" Command="{Binding DownCommand}" HorizontalAlignment="Left" Margin="215,73,0,0" VerticalAlignment="Top" Width="37"/>
</Grid>
MainViewModel:
public class MainViewModel : ViewModelBase
{
List<Customer> entityCustomer;
ObservableCollection<Customer> _CustomerOC;
public ObservableCollection<Customer> CustomerOC
{
get
{
return _CustomerOC;
}
set
{
Set(() => CustomerOC, ref _CustomerOC, value);
}
}
private Customer _SelectedItem;
public Customer SelectedItem
{
get
{
return _SelectedItem;
}
set
{
Set(() => SelectedItem, ref _SelectedItem, value);
}
}
private int _Id;
public int ID
{
get
{
return _Id;
}
set
{
Set(() => ID, ref _Id, value);
}
}
private string _FirstName = string.Empty;
public string FirstName
{
get
{
return _FirstName;
}
set
{
Set(() => FirstName, ref _FirstName, value);
}
}
private string _LastName = string.Empty;
public string LastName
{
get
{
return _LastName;
}
set
{
Set(() => LastName, ref _LastName, value);
}
}
public RelayCommand SearchCommand { get; private set; }
public RelayCommand UpCommand { get; private set; }
public RelayCommand DownCommand { get; private set; }
public RelayCommand SaveCommand { get; private set; }
public MainViewModel()
{
SearchCommand = new RelayCommand(() => Search());
UpCommand = new RelayCommand(() => Up());
DownCommand = new RelayCommand(() => Down());
SaveCommand = new RelayCommand(() => Save());
}
private void Search()
{
using (var ctx = new MyContext())
{
ctx.Configuration.LazyLoadingEnabled = false;
ctx.Configuration.ProxyCreationEnabled = false;
entityCustomer = ctx.Customers.ToList();
//datagrid
CustomerOC = new ObservableCollection<Customer>(entityCustomer);
}
}
private void Up()
{
var currentIndex = CustomerOC.IndexOf(SelectedItem);
if (currentIndex > 0)
{
int upIndex = currentIndex - 1;
//move the items
CustomerOC.Move(upIndex, currentIndex);
}
}
private void Down()
{
var currentIndex = CustomerOC.IndexOf(SelectedItem);
if (currentIndex + 1 < CustomerOC.Count)
{
int downIndex = currentIndex + 1;
//move the items
CustomerOC.Move(downIndex, currentIndex);
}
}
private void Save()
{
using (var ctx = new MyContext())
{
foreach (Customer c in entityCustomer)
{
ctx.Customers.Attach(c);
ctx.SaveChanges();
}
}
}
}
Continue reading...
MyContext:
public partial class Customer
{
public int ID { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
}
public class MyContext : DbContext
{
public MyContext()
: base("name=MyContext")
{
Database.SetInitializer<MyContext>(new CreateDatabaseIfNotExists<MyContext>());
}
public DbSet<Customer> Customers { get; set; }
}
XAML:
<Grid x:Name="LayoutRoot">
<DataGrid ItemsSource="{Binding CustomerOC, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding SelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="False" SelectionUnit="FullRow" SelectionMode="Single" IsReadOnly="True" HorizontalAlignment="Left" Margin="10,45,0,0" VerticalAlignment="Top" Height="214" Width="200">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding ID}" />
<DataGridTextColumn Header="FirstName" Binding="{Binding FirstName}" />
<DataGridTextColumn Header="LastName" Binding="{Binding LastName}" Width="*" />
</DataGrid.Columns>
</DataGrid>
<Button Content="Select" Command="{Binding SearchCommand}" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75"/>
<Button Content="Save" Command="{Binding SaveCommand}" HorizontalAlignment="Left" Margin="90,10,0,0" VerticalAlignment="Top" Width="75"/>
<Button Content="Up" Command="{Binding UpCommand}" HorizontalAlignment="Left" Margin="215,48,0,0" VerticalAlignment="Top" Width="37"/>
<Button Content="Down" Command="{Binding DownCommand}" HorizontalAlignment="Left" Margin="215,73,0,0" VerticalAlignment="Top" Width="37"/>
</Grid>
MainViewModel:
public class MainViewModel : ViewModelBase
{
List<Customer> entityCustomer;
ObservableCollection<Customer> _CustomerOC;
public ObservableCollection<Customer> CustomerOC
{
get
{
return _CustomerOC;
}
set
{
Set(() => CustomerOC, ref _CustomerOC, value);
}
}
private Customer _SelectedItem;
public Customer SelectedItem
{
get
{
return _SelectedItem;
}
set
{
Set(() => SelectedItem, ref _SelectedItem, value);
}
}
private int _Id;
public int ID
{
get
{
return _Id;
}
set
{
Set(() => ID, ref _Id, value);
}
}
private string _FirstName = string.Empty;
public string FirstName
{
get
{
return _FirstName;
}
set
{
Set(() => FirstName, ref _FirstName, value);
}
}
private string _LastName = string.Empty;
public string LastName
{
get
{
return _LastName;
}
set
{
Set(() => LastName, ref _LastName, value);
}
}
public RelayCommand SearchCommand { get; private set; }
public RelayCommand UpCommand { get; private set; }
public RelayCommand DownCommand { get; private set; }
public RelayCommand SaveCommand { get; private set; }
public MainViewModel()
{
SearchCommand = new RelayCommand(() => Search());
UpCommand = new RelayCommand(() => Up());
DownCommand = new RelayCommand(() => Down());
SaveCommand = new RelayCommand(() => Save());
}
private void Search()
{
using (var ctx = new MyContext())
{
ctx.Configuration.LazyLoadingEnabled = false;
ctx.Configuration.ProxyCreationEnabled = false;
entityCustomer = ctx.Customers.ToList();
//datagrid
CustomerOC = new ObservableCollection<Customer>(entityCustomer);
}
}
private void Up()
{
var currentIndex = CustomerOC.IndexOf(SelectedItem);
if (currentIndex > 0)
{
int upIndex = currentIndex - 1;
//move the items
CustomerOC.Move(upIndex, currentIndex);
}
}
private void Down()
{
var currentIndex = CustomerOC.IndexOf(SelectedItem);
if (currentIndex + 1 < CustomerOC.Count)
{
int downIndex = currentIndex + 1;
//move the items
CustomerOC.Move(downIndex, currentIndex);
}
}
private void Save()
{
using (var ctx = new MyContext())
{
foreach (Customer c in entityCustomer)
{
ctx.Customers.Attach(c);
ctx.SaveChanges();
}
}
}
}
Continue reading...