refresh after insert

Thread is closed for posting
2 posts, 0 answers
  1. Issam
    Issam avatar
    18 posts
    Member since:
    Sep 2008

    Posted 13 Dec 2013 Link to this post


    i am stll learning all thjese ORM concepts so ...

    in my wpf application i have a datagrid bound to a collectionviewsource,

    i am inserting records with a modal window with this code
    EditPatientForm ep = new EditPatientForm();
                PATIENT p = new PATIENT();
                ep.DataContext = p;
                if (ep.ShowDialog() == true)
                   if (ep.DialogResult == true)
                    var _context = ContextFactory.ObtainContext();


    if tried this code just after the savechange() method
                  System.Windows.Data.CollectionViewSource patientsViewSource =
    but i has no effect, i have to close the form and open it to see the changes .

    please note that i am using the same context via ContextFactory.ObtainContext() like suggested .
    and using this code to populate my datagrid
      private void UserControl_Loaded(object sender, RoutedEventArgs e)
       if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
                    System.Windows.Data.CollectionViewSource patientsViewSource =
                    var _context = ContextFactory.ObtainContext();
                    patientsViewSource.Source =  _context.PATIENTs.ToList();

    any help is welcome

    thanks and good day
  2. Kaloyan Nikolov
    Kaloyan Nikolov avatar
    118 posts

    Posted 17 Dec 2013 Link to this post


    As I reviewed your code I noticed that you initialize the ColelctionViewSource in the following way:
    patientsViewSource.Source =  _context.PATIENTs.ToList();

     This invokes a database call initializing patientsViewSource.Source with a collection of instances of the PATIENT class. Later on, you add a new item in the database through the Openaccess Context but not in the patientsViewSource.Source. This is the most probable reason why the page in not updated unless you reload it - patientsViewSource.Source =  _context.PATIENTs.ToList(); is invoked again and the page binding is redone.

    Possible approach to solving this problem would be to use ObservableCollection adding all new items both in the database through the context and also in the observable collection. The DataGrid should be databound to the ObservableCollection. More information on the difference between CollectionViewSource and ObservableCollection could be found in this forum post.

    In order to take advantage of the same functionality that GridViewCollection provides, you could use the CollectionViewSource.GetDefaultView(ObservableCollection) method.  For example:
    ICollectionView itemsViewSource = CollectionViewSource.GetDefaultView(this.

    Additionally, I would recommend to look through our Samples Kit which provides a number of real-life examples including various scenarios with WPF application using the OpenAccess ORM and MVVM pattern.

    Do not hesitate to contact us with any further questions.

    Kaloyan Nikolov
    OpenAccess ORM Q3 2013 simplifies your model operations even further providing you with greater flexibility. Check out the list of new features shipped with our latest release!
Back to Top