0
votes

I'm new to C#, MVVM, WPF and Entity Framework

my problem is, if I order the reading of the database (orderby) the datagrid will not be changed if I add a new row. if i do not order the reading it works.

so addition infos:

my database looks like this

Table Konto
KontoID
KontoName

Table Buchung
KontoID
BuchungsID
Name
BuchDate

after the Konto is loaded it will load all related Buchungen

 _entityKontoView = new CollectionViewSource();
 _entityBuchungView = new CollectionViewSource();
 // Loads the Konto
 _entityKontoView.Source = _database.Konto;

 _entityKontoView.View.CurrentChanged += (x, y) =>
 {
   _entityBuchungView.Source = ((Konto)_entityKontoView.View.CurrentItem).Buchung
            //.OrderBy(date => date.BuchDate)
                //.ThenBy(buchnr => buchnr.BuchungsID)
                ;
 };
 _entityKontoView.View.Refresh();

when i do now an OrderBy the datagrid will not be update after new inserted Row

i open the the database entity as follow:

public static databaseEntities _database = new databaseEntities();

my XAML binding on DataGrid:

ItemsSource="{Binding EntityBuchungsView.View}"

and my columns binding ;

Binding="{Binding Name}" 

as far i used _database.Buchung.AddObject(test); and then _database.SaveChanges(); and my DataGrid got updated.

what do i false?

1
You probably need to call .ToList() after the orderby (or any other call that enumerates the object, like AsEnumerable()). OrderBy() retuns a deferred execution object. - Kyeotic
just tried this, but nothing happen - domiSchenk

1 Answers

0
votes

if anyone have the same problem like me

i did implement a new method for refreshing the view:

public static void RefreshView(CollectionViewSource entity)
{
    entity.View.Refresh();
}

then it worked