I an trying to delete items from a GridView control. When I click delete, the GridView fails to update
I have bound a GridView with a list of orders in an observable collection. Here is my order class:
public class Order : INotifyPropertyChanged
{
public Order() { }
private int _id;
[PrimaryKey, AutoIncrement]
public int Id
{
get { return _id; }
set
{
this._id = value;
Notify("ID");
}
}
public int RestaurantID { get; set;}
public DateTime Date { get; set; }
public string Note { get; set; }
public decimal TotalPrice { get; set; }
[Ignore]
public List<OrderDetail> OrderDetails { get; set; }
#region INotifyPropertyChanged Members
[Ignore]
public event PropertyChangedEventHandler PropertyChanged;
[Ignore]
protected void Notify(string propName)
{
if (this.PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
#endregion
}
Here is my data binding code:
ObservableCollection<Order> orderCollection = new ObservableCollection<Order>();
foreach (Order ord in orders)
{
orderCollection.Add(ord);
}
this.DefaultViewModel["Items"] = orderCollection;
My delete button exists in the GridView’s DataTemplate. Here is my delete code:
private void btnDelete_Click_1(object sender, RoutedEventArgs e)
{
Button btnDelete = (Button)sender;
Order order = btnDelete.DataContext as Order;
repository.DeleteOrder(order.Id);
}
It looks like the code is only removing the item from the repository. You also need to remove it from the
ObservableCollection. Try adding this underrepository.DeleteOrder(order.Id):