0

all, I am working on a WPF application . In which I am using Data grid and it is bound to an Icollection customerCollection . I am using MVVM.

I have a button to add a new customer that shows a Dialog box by clicking it. through that Dialogbox I save data into my SQL server Database. Everything is ok but when Dialog box closes( CloseAction(); ). Datagrid does not update. What should I do? When I go back to any other menu item and click back on customer, Datagrid is updated, while I am calling the same function in constructor and in command execution. Images are attached for reference Any solution will be truly appreciated.

public CustomerViewModel()
        {            
            ShowNewCustomerWindowCommand = new ViewModelCommand(ExecuteShowNewCustomerWindowCommand);
            SearchCustomerCommand = new ViewModelCommand(ExecuteSearchCustomerCommand);
            GetData();            
        }

protected void GetData()
        {
            customer = new ObservableCollection<CustomerModel>();
            customer = customerRepository.GetByAll();
            customerCollection = CollectionViewSource.GetDefaultView(customer);
            customerCollection.Filter = FilterByName;
            customerCollection.Refresh();
            RaiseProperChanged();           
        }
private void ExecuteShowNewCustomerWindowCommand(object obj)
        {
            var addNewCustomer = new AddNewCustomer();
            addNewCustomer.ShowDialog();
        }

private void ExecuteSaveCustomerCommand(object obj)
        {
            customerModel.FirstName = FirstName;
            customerModel.LastName = LastName;
            customerModel.Contact = Contact;
            customerModel.Address = Address;
            customerRepository.Add(customerModel);
            CloseAction();
            GetData();
        }

moz bar
  • 1
  • 1

1 Answers1

1

i m just guessing because you did not post any xaml or properties. i assume you have at least one public property CustomerCollection and your datagrid is bind to it. I would change the code to the following:

public CustomerViewModel()
{     
   customer = new ObservableCollection<CustomerModel>(); 
   customerCollection = CollectionViewSource.GetDefaultView(customer);
   customerCollection.Filter = FilterByName;  

        ShowNewCustomerWindowCommand = new ViewModelCommand(ExecuteShowNewCustomerWindowCommand);
        SearchCustomerCommand = new ViewModelCommand(ExecuteSearchCustomerCommand);
        GetData();            
    }

public ICollectionView CustomerCollection {get; init;}

protected void GetData()
    {
        customer.Clear(); 
        customer.AddRange(customerRepository.GetByAll());
       
        customerCollection.Refresh();
     
    }
private void ExecuteShowNewCustomerWindowCommand(object obj)
    {
        var addNewCustomer = new AddNewCustomer();
        addNewCustomer.ShowDialog();
    }


private void ExecuteSaveCustomerCommand(object obj)
    {
        customerModel.FirstName = FirstName;
        customerModel.LastName = LastName;
        customerModel.Contact = Contact;
        customerModel.Address = Address;
        customerRepository.Add(customerModel);
        CloseAction();
        GetData();
    }

xaml

<DataGrid ItemsSource="{Binding CustomerCollection, Mode=OneWay}"
  • init ObservableCollection and ICollectionView once in ctor
  • use .Clear() instead of a new instance
  • addrange to add customerRepository.GetByAll()
blindmeis
  • 22,175
  • 7
  • 55
  • 74