How to reload the DataContext

datagridsilverlightsilverlight-4.0wcf-ria-services

I have a Silverlight Business Aplication (RIA Services) and I have a DataGrid attached to a DataSource's DataContext. In a Silverlight child Windows I create a new Entity and submit the changes to the server.
The problem is that my DataContext does not know that so the grid does not show the newly added entity.

How do I refresh the DataContext or tell the DataGrid to re-bind?

Edit: Here's my code

<riaControls:DomainDataSource AutoLoad="True" d:DesignData="{d:DesignInstance my:Team, CreateList=true}" Height="0" LoadedData="teamDomainDataSource_LoadedData" Name="teamDomainDataSource" QueryName="GetTeamsQuery" Width="0">
        <riaControls:DomainDataSource.DomainContext>
            <my:F1DomainContext />
        </riaControls:DomainDataSource.DomainContext>
</riaControls:DomainDataSource>

<sdk:DataGrid AutoGenerateColumns="False" Height="200" ItemsSource="{Binding ElementName=teamDomainDataSource, Path=Data}" Name="teamDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" DataContext="{Binding}">
                    <sdk:DataGrid.Columns>
                        <sdk:DataGridTextColumn x:Name="idColumn" Binding="{Binding Path=Id, Mode=OneWay}" Header="Id" IsReadOnly="True" Width="SizeToHeader" />
                        <sdk:DataGridTextColumn x:Name="nameColumn" Binding="{Binding Path=Name}" Header="Name" Width="SizeToHeader" />
                    </sdk:DataGrid.Columns>
</sdk:DataGrid>

As you can see my teamDataGrid 's ItemSource is the teamDomainDataSource defined above

Best Answer

This is an old question, but I had a similar problem and found that CanLoad was false. After investigating this with Reflector, I found that this is set only when the domain context has changes or is currently saving or loading (which was my case).

From the DomainDataSource class

private void UpdateCanLoadProperty()
{
    this.CanLoad = !this.IsSubmittingChanges && !this.HasChanges;
    if (this.HasChanges && this.IsLoadingData)
    {
        this.CancelLoadPrivate();
    }
}

And in the load method, you can see how it throws the error that you're receiving:

public void Load()
{
    if (!DesignerProperties.IsInDesignTool)
    {
        if (this.DomainContext == null)
        {
            throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, DomainDataSourceResources.OperationNeedsPropertySet, new object[] { "DomainContext", DomainDataSourceResources.LoadOperation }));
        }
        if (string.IsNullOrEmpty(this.QueryName))
        {
            throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, DomainDataSourceResources.OperationNeedsPropertySet, new object[] { "QueryName", DomainDataSourceResources.LoadOperation }));
        }
        if (this._loadDeferLevel > 0)
        {
            throw new InvalidOperationException(DomainDataSourceResources.LoadWithinDeferLoad);
        }
        this.ValidateQueryParameters();
        if (this._preparingOperation)
        {
            throw new InvalidOperationException(DomainDataSourceResources.InvalidOperationDuringLoadOrSubmit);
        }
        if (!this.CanLoad)
        {
            throw new InvalidOperationException(DomainDataSourceResources.CannotLoadWhenCanLoadIsFalse);
        }
        this.ExecuteLoad(this.InitialLoadType);
    }
}

To handle this error you'll need to submit your changes (or reject them, whichever is correct). If you're submitting your changes by using the SubmitChanges method you'll find that it contains an overload taking a callback which is where you want to call the Load method. In essence, you can't call load when there are changes.

Related Topic