locked
Refresh DataGrid from ViewModel fils MVVM WPF RRS feed

  • Question

  • Hi,

    I have  MainWindow and Window Add , ViewModel and ViewModelADD..l'addition of new Customer is correct in database, but the refresh of DataGrid no ! and when i finish l'addition , the Window ADD doesn't close !

    ViewModel:

           private static ViewModel1 instance = new ViewModel1();
            public static ViewModel1 Instance { get { return instance; } }
    		
    		
    		    private void add(object obj)
            {
    			Add addView = new Add();
                addView.DataContext = new ViewModelADD(loadDataBinding);
                addView.Show();
    		}
    		
    		 private ObservableCollection<Custmor> _loadDataBinding;     
    
            public ObservableCollection<Custmor> loadDataBinding
            {
                get
                {
                    return _loadDataBinding;
                }
    
                set
                {
                    _loadDataBinding = value;
                    OnPropertyChanged("loadDataBinding");
                }
            }

     

    ViewModelADD:

     public ViewModelADD(ObservableCollection<Custmor> loadDataBinding)
            {
                CustomerToAddObject = new Custmor();
    
                addCustomer1 = new RelayCommand(ADDFunction);
            }
    		
    		
    		 private ICommand addCustomer1;      
              public ICommand AddCustomer1
            {
                get { return addCustomer1; }
            }
    
           
            private void ADDFunction(object obj)
            {
                using (Test1Entities context = new Test1Entities())
                {
                    context.Custmor.Add(customerToAddObject);
                    context.SaveChanges();
                    MessageBox.Show("Customer a été ajouté avec succès!");
                }
    
                ViewModel1.Instance.loadDataBinding.Add(customerToAddObject);

    How can I fix it?
    to add new Customer..and refresh the DataGrid, and when the Customer added..the Window ADD close

    Thanks,



    Tuesday, May 23, 2017 3:19 PM

Answers

  • I found the solution,
    first I modify the ViewModel:

     private void add(object obj)
            {        
                Add addView = new Add();
                addView.DataContext = new ViewModelADD(addView,loadDataBinding);
                addView.Show();
            }
    

    then i modify the ViewModelADD:

             private readonly Window _window;       
            private readonly ObservableCollection<Custmor> _loadDataBinding;
    		
    		 public ViewModelADD(Window window,ObservableCollection<Custmor> loadDataBinding)
            {
                CustomerToAddObject = new Custmor();
    
                addCustomer1 = new RelayCommand(ADDFunction);
                _window = window;
                _loadDataBinding = loadDataBinding;
            }
    		
    		
    		   private void ADDFunction(object obj)
            {
                using (Test1Entities context = new Test1Entities())
                {
                    context.Custmor.Add(customerToAddObject);
                    context.SaveChanges();
                    MessageBox.Show("Customer a été ajouté avec succès!");
                }
                                    
                _loadDataBinding.Add(customerToAddObject);
                _window.Close();           
            }
    
    		
    		

    It works correct now,

    Thanks for your help

    • Marked as answer by Thabetidris Tuesday, May 23, 2017 4:51 PM
    Tuesday, May 23, 2017 4:51 PM

All replies

  • Post the code of "Custmor" class. Make sure it implements INotifyPropertyChanged interface

    https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx


    Gaurav Khanna | Microsoft MVP | Microsoft Community Contributor

    Tuesday, May 23, 2017 4:20 PM
  • Thanks,
    I made mapping with Entity Framwork..

    this is the class Custmor: 
     public partial class Custmor
        {
            public int ID { get; set; }
            public string nom { get; set; }
            public string prenom { get; set; }
            public Nullable<int> reference { get; set; }
        }
    But the adding in database is correct 
    Tuesday, May 23, 2017 4:35 PM
  • I found the solution,
    first I modify the ViewModel:

     private void add(object obj)
            {        
                Add addView = new Add();
                addView.DataContext = new ViewModelADD(addView,loadDataBinding);
                addView.Show();
            }
    

    then i modify the ViewModelADD:

             private readonly Window _window;       
            private readonly ObservableCollection<Custmor> _loadDataBinding;
    		
    		 public ViewModelADD(Window window,ObservableCollection<Custmor> loadDataBinding)
            {
                CustomerToAddObject = new Custmor();
    
                addCustomer1 = new RelayCommand(ADDFunction);
                _window = window;
                _loadDataBinding = loadDataBinding;
            }
    		
    		
    		   private void ADDFunction(object obj)
            {
                using (Test1Entities context = new Test1Entities())
                {
                    context.Custmor.Add(customerToAddObject);
                    context.SaveChanges();
                    MessageBox.Show("Customer a été ajouté avec succès!");
                }
                                    
                _loadDataBinding.Add(customerToAddObject);
                _window.Close();           
            }
    
    		
    		

    It works correct now,

    Thanks for your help

    • Marked as answer by Thabetidris Tuesday, May 23, 2017 4:51 PM
    Tuesday, May 23, 2017 4:51 PM