Click here to Skip to main content
15,901,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

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.

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:me="clr-namespace:MultiClientDb.ViewModels" Title="Window1" 
                  Height="401" Width="691" mc:Ignorable="d" 
        <CollectionViewSource x:Key="locationViewSource" 
                                d:DesignSource="{d:DesignInstance my:Location,
                                CreateList=True}" />
    <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}}" 
            VerticalAlignment="Top" Width="400">
                <DataGridTextColumn x:Name="trackNumberColumn"  
                                    Binding="{Binding Path=TrackNumber}" 
                                    Header="Track Number" 
                                    Width="SizeToHeader" />
                <DataGridTextColumn x:Name="iDColumn" 
                                    Binding="{Binding Path=ID}" 
                                    Width="SizeToHeader" />

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 = 

    locationViewSource.Source = m_dataContext.Locations; // Locations is the Tabelname

private void btRefresh_Click(object sender, RoutedEventArgs e)
    System.Windows.Data.CollectionViewSource locationViewSource =


Updated 15-Apr-12 23:04pm

1 solution

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.
Share this answer
Andy411 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)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900