IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Windows Presentation Foundation Discussion :

Modifier un Customer dans my DataGrid - WPF MVVM


Sujet :

Windows Presentation Foundation

  1. #1
    Nouveau membre du Club
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Décembre 2015
    Messages
    23
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Décembre 2015
    Messages : 23
    Points : 28
    Points
    28
    Par défaut Modifier un Customer dans my DataGrid - WPF MVVM
    Bonjour,

    J'ai un MainWindow contient DataGrid et aussi un Button Edit,

    XAML:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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:
    Nom : erreur.png
Affichages : 475
Taille : 194,0 Ko

    Et ceci la première exécution de mon application, et l'affichage est correcte depuis ma base de données:Nom : display.png
Affichages : 457
Taille : 61,5 Ko


    Merci pour votre aide d'avance,

  2. #2
    Nouveau membre du Club
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Décembre 2015
    Messages
    23
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Décembre 2015
    Messages : 23
    Points : 28
    Points
    28
    Par défaut
    J'ai trouvé la solution,

    il faut ajouter

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    windowUpdate.DataContext = this;
    dans la fonction start()

    Donc :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    private void start(object obj)
            {           
     
                Update windowUpdate = new Update();        
     
     
                windowUpdate.DataContext = this;
                windowUpdate.Show();
            }
    C'est la solution

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 6
    Dernier message: 26/10/2015, 08h09
  2. Enum dans un combobox WPF MVVM
    Par july4474 dans le forum Windows Presentation Foundation
    Réponses: 12
    Dernier message: 22/11/2011, 21h44
  3. Réponses: 2
    Dernier message: 01/03/2010, 21h32
  4. modifier une variable dans un datagrid
    Par frankbe dans le forum ASP.NET
    Réponses: 6
    Dernier message: 30/07/2008, 15h13
  5. [C#] Modifier et se déplacer dans un DataGrid
    Par fabrice1596 dans le forum Windows Forms
    Réponses: 4
    Dernier message: 16/09/2004, 22h10

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo