Reputation: 3
Introduction I am new in WPF and mvvm and therefore I try to master the basics by making a application of inventory management.
Tech-Stack
Problem Description
An Add
or Delete
operation doesn't automatically updates my datagrid
doesn't automatically updated.
I read lots of stuff on forums but nothing to do.
Code
ArticleViewModel
Class ArticleViewModel
{
private GStockEntities context;
public ArticleViewModel()
{
context = new GStockEntities();
}
public ObservableCollection<article> GetAllArticle()
{
ObservableCollection<article> Article = new ObservableCollection<article>();
try
{
var query = (from a in context.articles select a).ToList();
foreach (var item in query)
{
Article.Add(item);
}
}
catch (Exception)
{
throw;
}
return Article;
}
public ObservableCollection<article> Article
{
get
{
return this.GetAllArticle();
}
set
{
Set(ref _Article, value);
}
}
private void Add()
{
article Article = new article();
Article.id_article = id;
Article.nom = nom_Article;
Article.id_categorie = GetIdCategoriByName(nom_categorie);
Article.quantite = (int)qt;
Article.prix_achat = (decimal)prix_achat;
Article.prix_vente = (decimal)prix_vente;
context.articles.Add(Article);
context.SaveChanges();
}
}
ViewModelBase
class ViewModelBase : INotifyPropertyChanged, INotifyDataErrorInfo
{
public ViewModelBase()
{
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
public void Notify([CallerMemberName]string propertyName = null)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
protected bool Set<TProperty>(ref TProperty attrib,
TProperty value, [CallerMemberName]string propertyName = null)
{
if (!object.Equals(attrib, value))
{
OnPropertyChanging(propertyName, attrib, value);
attrib = value;
Notify(propertyName);
OnPropertyChanged(propertyName);
return true;
}
return false;
}
protected virtual void OnPropertyChanged(string propertyName) { }
protected virtual void OnPropertyChanging(string propertyName,
object oldValue, object newValue) { }
#endregion
}
ArticleView
<DataGrid DockPanel.Dock="Right"
AutoGenerateColumns="False"
Name="ArtDatagrid"
ItemsSource="{Binding Article}"
SelectedItem="{Binding SelectedArticle}"
CanUserAddRows="True">
<DataGrid.Columns>
<DataGridTextColumn
Header="Id"
Binding="{Binding id_article}"
Width="*"/>
<DataGridTextColumn
Header="Nom"
Binding="{Binding nom}"
Width="*"/>
<DataGridTextColumn
Header="Nom de la categorie"
Binding="{Binding nom_categorie}"
Width="*"/>
<DataGridTextColumn
Header="QT"
Binding="{Binding quantite}"
Width="*"/>
<DataGridTextColumn
Header="Prix d'achat"
Binding="{Binding prix_achat}"
Width="*"/>
<DataGridTextColumn
Header="prix de vente"
Binding="{Binding prix_vente}"
Width="*"/>
</DataGrid.Columns>
</DataGrid>
</DockPanel>
Upvotes: 0
Views: 976
Reputation: 4016
Because you add the new created article to the database but not to the ObservableCollection
that is bound to the grid.
One solution could be not to return the articles from the database directly in the getter. Instead you could use a local variable to store them:
private ObservableCollection<article> _article
public ObservableCollection<article> Article
{
get
{
if (_article == null)
_article = this.GetAllArticle();
return _article;
}
}
Then you can use that collection in the add method:
private void Add()
{
article Article = new article();
Article.id_article = id;
Article.nom = nom_Article;
Article.id_categorie = GetIdCategoriByName(nom_categorie);
Article.quantite = (int)qt;
Article.prix_achat = (decimal)prix_achat;
Article.prix_vente = (decimal)prix_vente;
context.articles.Add(Article);
context.SaveChanges();
_article.Add(Article);
}
Upvotes: 1