This is a migrated thread and some comments may be shown as answers.

refresh after insert

1 Answer 60 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Issam
Top achievements
Rank 1
Issam asked on 13 Dec 2013, 06:04 PM
hi,

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();
                _context.Add(p);
                _context.SaveChanges();
 
            }

 
 


if tried this code just after the savechange() method
              System.Windows.Data.CollectionViewSource patientsViewSource =
((System.Windows.Data.CollectionViewSource)(this.FindResource("pATIENTViewSource")));
              patientsViewSource.View.Refresh();
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 =
                    ((System.Windows.Data.CollectionViewSource)(this.FindResource("pATIENTViewSource")));
                var _context = ContextFactory.ObtainContext();
                patientsViewSource.Source =  _context.PATIENTs.ToList();
            }
}


any help is welcome

thanks and good day

1 Answer, 1 is accepted

Sort by
0
Kaloyan Nikolov
Telerik team
answered on 17 Dec 2013, 02:38 PM
Hello,

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.
ObservablePatients);

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.

Regards,
Kaloyan Nikolov
Telerik
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!
Tags
General Discussions
Asked by
Issam
Top achievements
Rank 1
Answers by
Kaloyan Nikolov
Telerik team
Share this question
or