views:

342

answers:

3

Hello,

I am creating a master detials silverlight page and am having trouble updating the datagrid after inserting a new record. On the top of the page I have text boxes to enter information, when a user clicks on save I would like the information automatically updated in the database and displayed on the datagrid without refreshing the screen.

I am able to save the information to the db with no problems its just trying to get the datagrid to refresh with the new changes.

Some of my code behind for the SAVE button is below:

ViewModel.UpdateWorkflow(summary, reason, Email);
LoadOperation<Document> lo = _Context.Load<Document>(_Context.GetDocumentsQuery(_DocID), rtRefresh, null);

The code for rtRefresh:

private void rtRefresh(LoadOperation<Document> oLoadOperation)
    {            
        if (oLoadOperation.IsComplete)
        {                                                         
            ViewModel.GetDocuments(_DocID);
        }
    }

I set the ViewModel in the xaml file as:

<controls:ChildWindow.Resources>
    <viewModel:DocumentChildWindowViewModel x:Key="ViewModel" />
</controls:ChildWindow.Resources>

And the ViewModel in the code-behind:

ViewModel = Resources["ViewModel"] as DocumentChildWindowViewModel;

Any help would be appreciated, thank you.

+1  A: 

I'm assuming your datagrid is bound to an IEnumerable, maybe a List<>? Have you tried using an ObservableCollection<>? If you are bound to an ObservableCollection, it will let the UI know when the collection has changed.

<UserControl x:Class="SilverlightApplication1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:SilverlightApplication1"
xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">

<UserControl.DataContext>
    <local:MainPage_ViewModel/>
</UserControl.DataContext>

<StackPanel>
    <data:DataGrid ItemsSource="{Binding MyCollection}">

    </data:DataGrid>
</StackPanel>

public class MainPage_ViewModel : INotifyPropertyChanged
{
    public ObservableCollection<object> MyCollection
    {
        get { return myCollection; }
        set 
        {
            if (myCollection != value)
            {
                myCollection = value;
                OnPropertyChanged("MyCollection");
            }
        }
    }
    private ObservableCollection<object> myCollection = new ObservableCollection<object>();

    public void LoadData()
    {
        //execute load method, the assign result to MyCollection
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    #endregion
}
JSprang
Is it just a matter of changing the GetDocumentsQuery() from IEnumerable<> to ObservableCollection<>?? Or is it more involved than this?
GPB
I would need to see you XAML and viewModel to be able to answer this. Your datagrid should have an ItemsSource that is bound to an ObservableCollection<> on your viewModel. When you get the data back from GetDocumentsQuery(), you should assign it to your ObservableCollection on your viewModel. Does that make sense?
JSprang
See edits to answer to see a simple example.
JSprang
Thanks for the help JSprang. I ended up figuring out my problem. I ended up not using the ObservableCollection<>, but you opened my eyes as to why I couldnt get this to work. In my VieModel I created a _Context = new DomainContext() when first loading and for some reason every time I tried to update the datagrid the old values just stayed their.So I ended up putting the _Context = new DomainContext() in the function that actually refreshed the datagrid and it worked!
GPB
A: 

In my ViewModel I initially create a new DomainContext():

public DocumentWindowViewModel()
    {
        _Context = new appDomainContext();                       
        WireCommands();
    }

I ended up having to copy the following line to my query:

_Context = new appDomainContext();

And paste to the function that grabs the values from the database:

public void GetDocuments(int dID)
    {           
            _Context = new appDomainContext();  //ADDED THIS LINE TO FIX MY ISSUE                              
            _Context.Load(_Context.GetDocumentsQuery(dID), dlo =>
                {
                    DocumentsView = dlo.Entities;                                                
                }, null);                                
        }
    }

the _Context was keeping old values. This cleared the _Context and allowed me to have the updated data in the _Context.

GPB
A: 

Your problem could be related to the default LoadBehavior for the domain context Load() method. Try setting the LoadBehaviour explicitly to LoadBehaviour.RefreshCurrent. For more on LoadBehaviour please see: http://msdn.microsoft.com/en-us/library/system.servicemodel.domainservices.client.loadbehavior(v=VS.91).aspx.

Sometimes, for example after a delete, you will need to also Clear() the EntityContainer on the domain context or atleast Clear() the particular EntitySet<> for the entity you are trying to load, for the right entities to be displayed.

like, MyDomainContext.EntityContainer.GetEntitySet().Clear();

dev.bv