question

MarcJeeves-9615 avatar image
0 Votes"
MarcJeeves-9615 asked ·

wpf datagrid not updating after change to observable collection

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-wpf
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

PeterFleischer-3316 avatar image
1 Vote"
PeterFleischer-3316 answered ·

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);
 }


· 2 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I tried this earlier and get this error, that's why i did it in two steps.

Severity Code Description Project File Line Suppression State
Error CS0029 Cannot implicitly convert type 'void' to 'System.Collections.ObjectModel.ObservableCollection<InfarstructureProject.Models.ContinentModel>' ImplementationProject C:\Users\ma_je\Desktop\LekoCustom\LekoCustomSolution\ImplementationProject\Implementation\CustomDataContinentImplementation.cs 135 Active

0 Votes 0 · ·

Hi, show code in line 135.

0 Votes 0 · ·
MarcJeeves-9615 avatar image
2 Votes"
MarcJeeves-9615 answered ·

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

this works once i added this

· 3 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,
I cannot see this lines in your code.

0 Votes 0 · ·

Hello. This is how my wpf app works:

View:

 <DataGrid ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}" />
 <Button Command="{Binding Button_Update}" />

ViewModel:

 public ObservableCollection<T> Items { get; private set; }

 public T SelectedItem
 {
     get => GetValue<T>();
     set => SetValue(value, OnSelectedItemChanged);
 }
 ...
 Button_Update = new DelegateCommand(() =>
 {
      UpdateSelectedItem();
    
      DbContext.Update(SelectedItem);
      DbContext.SaveChanges();
    
      CollectionViewSource.GetDefaultView(Items).Refresh();
 }
1 Vote 1 · ·

Thanks, it really works!

0 Votes 0 · ·