wpf datagrid not updating after change to observable collection

Marc Jeeves 386 Reputation points
2020-03-21T15:01:59.763+00:00

Hi All,

My Delete and add methods work fine, the Update is calling the HTTPPut and changing the data but i cant get the WPF form to update the same way i have been updating with add and delete.

My class is implementing INotifyPropertyChanged and i have the OnPropertyChanged method.

is there a better way to update the observable collection for add and delete that will fix update?
Thanks
Madaxe

//----------------------------------------------------------

protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

//----------------------------------------------------------

        public void UpdateContinent(string iNewContinentName, int iContinentObjectId)
        {
            Task<ContinentModel> TaskContinentModel = this.UpdateExistingContinent(iNewContinentName, iContinentObjectId);
            ObservableCollection<ContinentModel> NewContinentModels = _ContinentModels;
            ContinentModel UpdatedContinentModel = TaskContinentModel.Result;

            NewContinentModels.Where(w => w.objectID == UpdatedContinentModel.objectID).ToList().ForEach(i => i.continentName = iNewContinentName);

            this.ContinentModels = NewContinentModels;
        }

        private async Task<ContinentModel> UpdateExistingContinent(string iNewContinentName, int iContinentObjectId)
        {
            ContinentModel ReturnContinentModel = null;

//----------------------------------------------------------

        public void CreateContinent(string iContinentName)
        {
            Task<ContinentModel> TaskContinentModel = this.CreateNewContinent(iContinentName);
            ObservableCollection<ContinentModel> NewContinentModels = _ContinentModels;
            NewContinentModels.Add(TaskContinentModel.Result);
            this.ContinentModels = NewContinentModels;
        }


        private async Task<ContinentModel> CreateNewContinent(string iContinentName)
        {
            ContinentModel ReturnContinentModel = null;

//----------------------------------------------------------

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,676 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Marc Jeeves 386 Reputation points
    2020-03-22T10:56:06.703+00:00

    CollectionViewSource.GetDefaultView(DGD_UserList.ItemsSource).Refresh();

    this works once i added this

    2 people found this answer helpful.

  2. Peter Fleischer (former MVP) 19,231 Reputation points
    2020-03-21T20:07:10.047+00:00

    Hi, change your update method (assign !)

    public void UpdateContinent(string iNewContinentName, int iContinentObjectId)
    {
    Task<ContinentModel> TaskContinentModel = this.UpdateExistingContinent(iNewContinentName, iContinentObjectId);
    ObservableCollection < ContinentModel> NewContinentModels = _ContinentModels;
    ContinentModel UpdatedContinentModel = TaskContinentModel.Result;
    
    this.ContinentModels = NewContinentModels.Where(w => w.objectID == UpdatedContinentModel.objectID).ToList().ForEach(i => i.continentName = iNewContinentName);
    }
    
    1 person found this answer helpful.