2 pièce(s) jointe(s)
Modifier un Customer dans my DataGrid - WPF MVVM
Bonjour,
J'ai un MainWindow contient DataGrid et aussi un Button Edit,
XAML:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <DataGrid x:Name="EmpDataGrid"
Grid.Row="1"
AutoGenerateColumns="False"
ItemsSource="{Binding loadDataBinding,Mode=TwoWay}"
SelectedItem="{Binding CurrentCustomer}" Grid.ColumnSpan="2">
<DataGrid.Columns>
<DataGridTextColumn Header="CustmorID" Binding="{Binding ID}" />
<DataGridTextColumn Header="CustmorNom" Binding="{Binding nom}" />
<DataGridTextColumn Header="CustmorPrenom" Binding="{Binding prenom}" />
<DataGridTextColumn Header="CustmorReference" Binding="{Binding reference}" />
</DataGrid.Columns>
<Button
Content="Edit"
Command="{Binding Edit}"
CommandParameter="{Binding CurrentCustomer}" /> |
Puis je clique sur la bouton Edit to modifier une customer selectionné, XAML Window Edit :
Code:
1 2 3 4
| <Button Command="{Binding UpdateCustomer}"
<TextBox x:Name="nom" Text="{Binding CustomerToAddObject.nom,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
<TextBox x:Name="prenom" Text="{Binding CustomerToAddObject.prenom,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" ></TextBox>
<TextBox x:Name="reference" Text="{Binding CustomerToAddObject.reference,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" ></TextBox> |
ViewModel:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| class ViewModel1 : ViewModelBase
{
Test1Entities context = new Test1Entities();
public ViewModel1()
{ using (Test1Entities context = new Test1Entities())
{
_loadDataBinding = new ObservableCollection<Custmor>(context.Custmor.ToList());
}
edit = new RelayCommand(start);
CustomerToAddObject = new Custmor();
updateCustomer = new RelayCommand(UpdateFunction);
}
private ICommand edit;
public ICommand Edit
{
get
{
return edit;
}
}
//To call Update Window:
private void start(object obj) {
Update windowUpdate = new Update();
windowUpdate.Show();
}
private ICommand updateCustomer;
public ICommand UpdateCustomer
{
get { return updateCustomer; }
}
private void UpdateFunction(object obj)
{
MessageBox.Show(currentCustomer.nom);
MessageBox.Show(customerToAddObject.nom);
using (Test1Entities context = new Test1Entities())
{
Custmor cus = context.Custmor.Find(currentCustomer.ID);
cus.nom = customerToAddObject.nom;
cus.prenom = customerToAddObject.prenom;
cus.reference = customerToAddObject.reference;
context.SaveChanges();
}
}
// customerToAddObject
private Custmor customerToAddObject;
public Custmor CustomerToAddObject
{
get { return customerToAddObject; }
set { customerToAddObject = value; }
}
//CurrentCustomer
private Custmor currentCustomer;
public Custmor CurrentCustomer
{
get { return currentCustomer; }
set
{ currentCustomer = value;
OnPropertyChanged("CurrentCustomer");
}
} |
Quand* j'exécute,j'ai cette erreur:
Pièce jointe 278026
Et ceci la première exécution de mon application, et l'affichage est correcte depuis ma base de données:Pièce jointe 278030
Merci pour votre aide d'avance,