Sharing data between multiple scopes

Thread is closed for posting
3 posts, 0 answers
  1. Marcus
    Marcus avatar
    5 posts
    Member since:
    Sep 2012

    Posted 04 Sep 2009 Link to this post

    Hi,

    I'm doing something which seems like it should be really simple, but for some reason I can't get it to work.

    I'm using an ObjectProvider and an ObjectView to bind to a RadGridView, and that works fine.  Editing data from within the grid also works fine.  But there are other parts of my application that use a different object scope to modify data, and for the life of me I can't get the RadGrid to update to show this data without closing my application and starting it again.

    I've tried using the ObjectProvider.Refresh() method, followed by the RadGridView.Refresh() method, but that didn't work.
    I even tried calling the ObjectView.PerformRefresh() method directly, but that didn't work either.

    Can someone give me a hint as to how to best make this work?

    Thanks,

    Marcus.
  2. Marcus
    Marcus avatar
    5 posts
    Member since:
    Sep 2012

    Posted 07 Sep 2009 Link to this post

    As a follow up to this question, I think the problem is not that the refresh is not working, but that the refresh does not work on edits to existing items, only new item additions or removals.

    I can see new items that are added to the database, and they show up in the grid just fine when using the Refresh() method.  But when edits are made in a different scope to existing items and then committed to the database, these changes are not reflected in the grid when the Refresh() method is called.

    I can even see the edited data properly when using a different scope.  But when I debug to look into the data inside the ObjectProvider, it shows the old version of the data.

    To me it looks like there may be a bug in the ObjectProvider class, especially to do with the Refresh() method.

    Can someone confirm this for me?  Or is there something else that I should be doing?

    Thanks,

    Marcus.
  3. PetarP
    Admin
    PetarP avatar
    754 posts

    Posted 10 Sep 2009 Link to this post

    Hi Marcus,

    This is a copy of our answer to the support ticket you sent us, so if anyone faces the same problem, can find a possible solution below.

    It is possible to update the data outside the form but the problem is that you have to use the same object scope that the ObjectProvider is using in order to benefit from the tracking events of this scope. These events can be used to refresh the grid content automatically. The code looks like this:
    public partial class Form1 : Form  
    {  
        public Form1()  
        {  
            InitializeComponent();  
      
            objectProvider1.Context.Tracking.Changed += new ChangeEventHandler(Tracking_Changed);  
        }  
      
        void Tracking_Changed(object sender, ChangeEventArgs e)  
        {  
            objectProvider1.SaveAll();  
            objectProvider1.Refresh();  
        }  
      
        // add a new ORMData1 to the database  
        private void button1_Click(object sender, EventArgs e)  
        {  
            ...  
        }  
      
        // increment the Val1 of the first item in the list by 1 and save it to the database  
        private void radButton1_Click(object sender, EventArgs e)  
        {  
            IObjectScope scope = ObjectScopeProvider1.ObjectScope();  
      
            if (!scope.Transaction.IsActive)  
            {  
                scope.Transaction.Begin();  
            }  
            ORMData1 data = scope.Extent<ORMData1>().First();  
            data.Val1++;  
      
            scope.Transaction.Commit();  
            scope.Transaction.Begin();  
        }  
      
    }  
    Note that we have set the objectProviders1's UsePrivateObjectContext property to False and the same ObjectScopeProvider1 is used to obtain a scope for outer updates.

    If you do not want to use a scope shared between the object provider and the rest of the application, you will have to manually 'tell' the form to refresh the grid content (execute the SaveAll() and Refresh() methods), which is not so nice as you said.

    Sincerely yours,
    Petar
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Back to Top