Search code examples
c#wpfsqliteentity-frameworkdata-binding

WPF Datagrid is not updating when call SaveChanges() in Entity Framework


I have small WPF / .NET application and connected via System.Data.Entity to a SQLite database. MainWindow has a button and a textbox.

When the button is clicked, a new element with id must be added to the database. It works and updates the datagrid correctly. But if element with current id already exists, count must be increment instead. This changes correctly apply to the database, but not to the data grid.

Datagrid value update only when I scroll down until it hides and bring it back. How can I fix it correctly?

The MainWindow.xaml.cs code is the following:

public partial class MainWindow : Window
{
    private ProductContext _context;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Product newProduct = new Product() {
            Id = idTextBox.Text,
            Count = 1
        };

        Product oldProduct = _context.Products.Find(newProduct.Id);
        if(oldProduct!= null)
            oldProduct.Count++;
        else
            _context.Products.Add(newProduct);

        _context.SaveChanges();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        _context = new ProductContext();

        _context.Products.Load();
        dataGrid.ItemsSource = _context.Products.Local;
    }

    private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        _input.Dispose();
    }
}

Code of DataGrid in MainWindow.xaml:

<DataGrid AutoGenerateColumns="False" Name="dataGrid">
    <DataGrid.Columns>
        <DataGridTextColumn Header="ID" Binding="{Binding Id}"/>
        <DataGridTextColumn Header="COUNT" Binding="{Binding Count}"/>
    </DataGrid.Columns>
</DataGrid>

Database context:

public partial class ProductContext : DbContext
{
    public DbSet<Product> Products { get; set; }

    public ProductContext () : base("name=ProductDB") { }
}

Product:

public class Product
{
    public string Id { get; set; }
    public int Count { get; set; }
}

I have tried adding _context.Products.Load() and few other bad ones, but nothing changes.

PS: I will be very grateful for advice if I fundamentally do something wrong when communicating with the database


Solution

  • I found a solution quite by accident. If the class Product implements INotifyPropertyChanged interface, then the datagrid is updated correctly

    public class Product : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
        public string Id { get; set; }
    
        private int _count;
        public int Count 
        { 
            get
            {
                return _count;
            }
            set
            {
                _count = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Count)));
            }
        }
    }