To load items that belong to a selected order
Open the MainPage.xaml.cs source code file of the Silverlight project.
In the public constructor method for the MainPage class, insert the following code that registers a handler for the SelectionChanged event on the Orders DataGrid.
' Register a handler for the SelectionChanged event on the orders grid.
AddHandler ordersGrid.SelectionChanged, AddressOf ordersGrid_SelectionChanged
// Register a handler for the SelectionChanged event on the orders grid.
ordersGrid.SelectionChanged +=
new SelectionChangedEventHandler(ordersGrid_SelectionChanged);
Add the following methods to the MainPage class. These methods are called when an order is selected in the ordersGrid. The LoadDetails method calls the BeginLoadProperty method on the context to load Order_Details objects that are related the currently selected Orders object. The OnOrderDetailsLoaded method handles the result of this asynchronous operation by calling EndLoadProperty and adding the returned Order_Details objects into an ObservableCollection<(Of <(T>)>) that is bound to a second DataGrid.
Private Sub ordersGrid_SelectionChanged(ByVal sender As Object, _
ByVal e As SelectionChangedEventArgs)
LoadDetails(ordersGrid.SelectedItem)
End Sub
Private Sub LoadDetails(ByVal order As Object)
' Define the delegate to callback into the process
Dim callback As AsyncCallback = AddressOf OnOrderDetailsLoaded
' Reset the order binding collection.
detailsBindingCollection.Clear()
If order IsNot Nothing Then
Try
' Begin the load related items.
svcContext.BeginLoadProperty(order, "Order_Details", _
callback, Nothing)
Catch ex As Exception
messageTextBlock.Text = ex.Message
End Try
End If
End Sub
Private Sub OnOrderDetailsLoaded(ByVal result As IAsyncResult)
' Persist the result for the delegate.
currentResult = result
' Use the Dispatcher to ensure that the
' asynchronous call returns in the correct thread.
Dispatcher.BeginInvoke(AddressOf OrderDetailsLoadedByDispatcher)
End Sub
Private Sub OrderDetailsLoadedByDispatcher()
Try
For Each item In svcContext.EndLoadProperty(currentResult)
' Load the item into the binding collection.
detailsBindingCollection.Add(item)
Next
' Reset and rebind the grid to the items binding collection
' and update the layout.
Me.detailsGrid.DataContext = detailsBindingCollection
Me.detailsGrid.UpdateLayout()
Me.detailsGrid.Columns(5).Visibility = Visibility.Collapsed
Me.detailsGrid.Columns(6).Visibility = Visibility.Collapsed
Catch ex As DataServiceQueryException
messageTextBlock.Text = String.Format("Error: {0} - {1}", _
ex.Response.StatusCode.ToString(), ex.Response.Error.Message)
End Try
End Sub
private void ordersGrid_SelectionChanged(object sender,
SelectionChangedEventArgs e)
{
LoadDetails(ordersGrid.SelectedItem);
}
private void LoadDetails(object order)
{
// Reset the order binding collection.
detailsBindingCollection.Clear();
if (order != null)
{
try
{
// Begin the load related items.
svcContext.BeginLoadProperty(order, "Order_Details",
OnOrderDetailsLoaded, null);
}
catch (Exception ex)
{
messageTextBlock.Text = ex.Message;
}
}
}
private void OnOrderDetailsLoaded(IAsyncResult result)
{
// Use the Dispatcher to ensure that the
// asynchronous call returns in the correct thread.
Dispatcher.BeginInvoke(() =>
{
try
{
foreach (Order_Details item in svcContext.EndLoadProperty(result))
{
// Load the item into the binding collection.
detailsBindingCollection.Add(item);
}
// Reset and rebind the grid to the items binding collection
// and update the layout.
this.detailsGrid.DataContext = detailsBindingCollection;
this.detailsGrid.UpdateLayout();
this.detailsGrid.Columns[5].Visibility = Visibility.Collapsed;
this.detailsGrid.Columns[6].Visibility = Visibility.Collapsed;
}
catch (DataServiceQueryException ex)
{
messageTextBlock.Text = string.Format("Error: {0} - {1}",
ex.Response.StatusCode.ToString(), ex.Response.Error.Message);
}
}
);
}
To build and run the application
From the Debug menu, select Start Debugging or Start Without Debugging.
This builds and starts the application.
When the page loads, enter a value in the Customer ID text box (a valid value of ALFKI is provided), and then click Get Orders.
This displays the orders that belong to that customer.
Click on a different order in the Orders grid.
This displays the items for the selected order.
You have successfully enhanced the client application for Silverlight by adding the ability to load Order_Details objects that are related to a selected Order object. Next, you will update and delete existing Order_Details objects and save these changes to the data service:
Modifying and Deleting Entity Data
Other Resources