alice7
alice7

Reputation: 3880

MVVM is not refreshing datagrid contents

I have a class testviewModel which has implemented INotifychanged event and all its proeprties.

public class testViewModel:INotifychanged
{
    public string ServiceTag
    {
        get { return _serviceTag; }
        set { _serviceTag = value;
        PropertChanged("ServiceTag");}
    }


}
public event PropertyChangedEventHandler PropertyChanged;

    private void PropertChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

And I have a datagrid which is binding to the properties of the viewModel.

On the window load Im binding using like this:

DataGrid.ItemsSource = ObservableCollection;

And it shows the records in the datagrid with the properties of teh testViewModel.

But when I fire the delete event, it deletes from the database but it doesn't update the UI or refresh datagrid.

I am calling DataGrid.Items.Refresh();

Do I have to specifically remove from the observable collection? Is there anything I have to do in xaml?

Upvotes: 0

Views: 2530

Answers (2)

Muhamed Shafeeq
Muhamed Shafeeq

Reputation: 1214

update your ObservableCollectionmembers too
Eg: 

private ObservableCollection<Member> memberCollection;
        public ObservableCollection<Member> MemberCollection
        {
            get { return memberCollection; }
            set { memberCollection = value;
                OnPropertyChanged();
            }
        }

public void SaveMember()
        {
            try
            {
                _bussiness.Update(SelectedMember);
                MemberCollection.Add(SelectedMember);
                ShowMessageBox(this, new MessageEventArgs()
                {
                    Message = "Changes are saved !"
                });
            }
            catch (Exception ex)
            {
                ShowMessageBox(this, new MessageEventArgs()
                {
                    Message = ex.Message
                });
            }
        }

Upvotes: 0

Ed Swangren
Ed Swangren

Reputation: 124790

Do I have to specifically remove from the observable collection?

Yes, because that is what the DataGrid is bound to. It could not possibly know that you have deleted a record from the database. You could also change out the collection completely as long as the property itself raises a PropertyChanged event, but you should simply remove the item from the collection.

Upvotes: 2

Related Questions