alice7 alice7 - 4 years ago 103
C# Question

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;

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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download