I am looking for solution to the issue of insert new or delete of a datagrid row that is not being identified or captured by simply checking dbcontext.changetracker.haschanges status. HasChanges picks up when user modifies existing datagrid row but (as I have read) insert and delete on a datagrids itemssource that is set using a collectionviewsource as in my case is not recognized by HasChanges. The numerous discussions on internet of this have so far not led me to the solution which must be a commmon one if this design is itself common. The reason for this is me and my lack of knowledge (this is my first WPF, MVVM EF app), so I would like to get a working example to review and learn from.
I bring in a list of items from the database that are used to populate combobox itemssource. The user may want add to, remove or modify this list so I have another view from which they can maintain this list. I can deal with the modify action because well thats automatically recognized when I use HasChanges, but am lost on how to identify that an insert or delete of a row has occurred. If I can understand how to determine or "capture" when this has occurred then I believe I know how to add/remove this from its class and can then save it to the database.
What I have tried:
I have tried all sorts of different binding options, xaml design of the datagrid and associated columns, read about messenger and Services. Appears that the more I look through internet the more complicated it appears.
One of my datagrids is as follows;
<pre> <DataGrid x:Name="siteDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True" Grid.Column="0" Grid.Row="3"
ItemsSource="{Binding GeneralSitesViewSource, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Margin="10,10,10,1" RowDetailsVisibilityMode="VisibleWhenSelected"
CellStyle="{StaticResource datagridcellstyle}" Height="110" Width="330">
<DataGrid.Columns>
<DataGridTextColumn x:Name="Site1Column" Binding="{Binding Site1}" Header="Site Name" Width="310"/>
</DataGrid.Columns>
</DataGrid>
Datagrid ItemsSource created as follows;
private void FillGeneralSites()
{
var q = (from a in Generalcontext.Sites
select a).ToList();
_generalSites = new ObservableCollection<Site>(q);
GeneralSitesViewSource = CollectionViewSource.GetDefaultView(_generalSites);
GeneralSitesViewSource.SortDescriptions.Add(new SortDescription("Site1", ListSortDirection.Ascending));
}
public ICollectionView GeneralSitesViewSource { get; set; }
private ObservableCollection<Site> _generalSites;
public ObservableCollection<Site> GeneralSites
{
get
{
return _generalSites;
}
set
{
_generalSites = value;
OnPropertyChanged("GeneralSites");
}
}
Currently set up a check by using HasChanges only and if any modifications in which case save
public bool CanClose()
{
if (Generalcontext.ChangeTracker.HasChanges())
{
MessageBoxResult msgresult = MessageBox.Show("Changes were not saved. Do you want to save your changes before exiting?", "EXIT WINDOW",
MessageBoxButton.YesNoCancel, MessageBoxImage.Warning);
if (msgresult == MessageBoxResult.Yes)
{
Generalcontext.SaveChanges();
return true;
}
else if (msgresult == MessageBoxResult.No)
{
return true;
}
else
{
return false;
}
}
else
{
return true;
}
}