Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF C#4.0 EF4.0
Hi all,
 
First time I'm asking a question here. Hope I did it correct.
 
I have an appliaction (WPF) which uses EF to connect to a database. My need is to detect changes of the database which are performed by external apps and then to reload the data in my application to be up to date. The WPF app uses a Datagrid which is binded to the database using EF.
 
In fact I Have two questions:
 
1)
When I'm manualy refreshing the data, only changed rows are updated in the grid. Neither new rows are schown nor deleted rows are removed. For me, it looks like I have an error in my binding, but I don't see it. Maybe someone can give me a hint.
 
2)
How to automatically detect changes of the database?
I know that all queries are up to date as soon as they are executed. but my attempt is not to poll the database. Instead I want an event. Sth. like consuming an event ObjectContext.DbChanged, but there is no such event (or I did not find it?). Is there any "easy" way to detect changes of the database with entity framework? Or at least, do I realy have to poll the database?
 
Of course, I asked google, but maybe I'm asking the question in the wrong way, so the answers don't satisfy me.
 
Any kind of hints are appreciated.
 
The database has a table Locations with the rows ID and TrackNumber.
 
Here is my XAML code
<Window x:Class="MultiClientDb.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:me="clr-namespace:MultiClientDb.ViewModels" Title="Window1" 
                  Height="401" Width="691" mc:Ignorable="d" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        xmlns:my="clr-namespace:EFDataAccess;assembly=EFDataAccess">
    <Window.Resources>
        <CollectionViewSource x:Key="locationViewSource" 
                                d:DesignSource="{d:DesignInstance my:Location,
                                CreateList=True}" />
    </Window.Resources>
    <Grid DataContext="{StaticResource locationViewSource}">        
        <Button Content="Refresh" Height="23" HorizontalAlignment="Left" 
                         Margin="568,152,0,0" Name="btRefresh"
                         VerticalAlignment="Top" Width="75" 
                         Click="btRefresh_Click" />
        <DataGrid AutoGenerateColumns="False" EnableRowVirtualization="True" 
            Height="200" HorizontalAlignment="Left" 
            ItemsSource="{Binding Source={StaticResource locationViewSource}}" 
            Name="locationDataGrid" 
            RowDetailsVisibilityMode="VisibleWhenSelected" 
            VerticalAlignment="Top" Width="400">
            
            <DataGrid.Columns>
                <DataGridTextColumn x:Name="trackNumberColumn"  
                                    Binding="{Binding Path=TrackNumber}" 
                                    Header="Track Number" 
                                    Width="SizeToHeader" />
                <DataGridTextColumn x:Name="iDColumn" 
                                    Binding="{Binding Path=ID}" 
                                    Header="ID" 
                                    Width="SizeToHeader" />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>
 
 

And here's the C# code
private MultiClientModelContainer m_dataContext = new MultiClientModelContainer();
 
private void Window_Loaded(object sender, RoutedEventArgs e)
{
    System.Windows.Data.CollectionViewSource locationViewSource = 
       ((System.Windows.Data.CollectionViewSource)
       (this.FindResource("locationViewSource")));
 
    locationViewSource.Source = m_dataContext.Locations; // Locations is the Tabelname
}
 

private void btRefresh_Click(object sender, RoutedEventArgs e)
{
    System.Windows.Data.CollectionViewSource locationViewSource =
       ((System.Windows.Data.CollectionViewSource)
       (this.FindResource("locationViewSource")));
 
    m_dataContext.Refresh(
        System.Data.Objects.RefreshMode.StoreWins, 
        m_dataContext.Locations);
 
    BindingOperations.GetBindingExpression(
        locationDataGrid, 
        DataGrid.ItemsSourceProperty).UpdateTarget();
}
Posted 15-Apr-12 22:40pm
Andy4112.6K
Edited 15-Apr-12 23:04pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

hi
 
I think if you fill data from entity to list of type ObservableCollection and set the itemsource of datagrid to ObservableCollection list,your problem will be solved.
  Permalink  
Comments
Andy411 at 16-Apr-12 9:24am
   
Thank You for your sugestion.
I also thought of an ObservableCollection but then I allways have to update the collection fom the database, including removed and added rows. Also I have to write user changes from the collection to the database.
I'm sure there must be an easier way to solve my problem. Row updates are supported in both ways, only adding and deleting does not wirk as excpected.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 16 Apr 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid