Click here to Skip to main content
15,888,802 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:

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
HTML
<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
C#
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
Updated 15-Apr-12 23:04pm
v2

1 solution

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