sebagomez
sebagomez

Reputation: 9609

How to reload the DataContext?

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

Upvotes: 2

Views: 6714

Answers (4)

Matt Ruwe
Matt Ruwe

Reputation: 3406

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.

Upvotes: 1

sebagomez
sebagomez

Reputation: 9609

So after following @Kyle suggestion I got the following error:

A load operation cannot be performed when CanLoad is false. Controls that invoke load operations should be disabled when CanLoad is false

So I googled around and I found this post which showed a "not too pretty" way of working this around... but it does the trick.

So this is how I fixed it...

teamDataGrid.ItemsSource = null;
teamDataGrid.ItemsSource = ((F1DomainContext)teamDomainDataSource.DomainContext).Teams;

So what I did was rebinding the datasource to my grid... does anybody know a prettier way?

Upvotes: 0

Kyle McClellan
Kyle McClellan

Reputation: 2029

Your two best options are to either (1) reload the DDS using DomainDataSource.Load() to get the latest data or (2) add the entity via the DDS before submitting it using DomainDataSource.DataView.Add().

Upvotes: 0

user118190
user118190

Reputation: 2189

Make sure your Datagrid's "List" is of type: ObservableCollection. Easy to implement and allows for such notification.

Let me know if you need further assistance.

Upvotes: 0

Related Questions