views:

40

answers:

3

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

A: 

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.

hey, don't know if I understood your suggestion... what do I need to change? I added a little code so can see what I'm talking about
sebastian
Oh, I see, I didn't notice you were using RIA Services. So, let me ask, your query, I would think something like public ObservableCollection<Team> GetTeamsQuery()... could you post some more code (especially the code-behind)? Also, when you add a new object via the childWindow, make sure you are adding the collection and it should replicate through.
A: 

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().

Kyle McClellan
Hello Kyle, I preferr doing .Load() but I got the following error message, which I'm about google: A load operation cannot be performed when CanLoad is false. Controls that invoke load operations should be disabled when CanLoad is false. Any advise?
sebastian
More or less you can't reload when there are uncommitted changes (you wouldn't want to lose them, right?). Typically this is related to DDS.DomainContext.HasChanges.
Kyle McClellan
A: 

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?

sebastian