Export (0) Print
Expand All

Loading Related Entities (WCF Data Services/Silverlight)

Silverlight

In this task, you will extend the code from the previous task to also load Order_Details objects that are related to the selected Orders object. In the method that handles the SelectionChanged event, which is raised by the orders DataGrid, you call the BeginLoadProperty method on the DataServiceContext to asynchronously load Order_Details objects that are related to the Order object that is selected in the grid. These orders are added to an ObservableCollection<T> that is bound to a second data grid.

To load items that belong to a selected order

  1. Open the MainPage.xaml.cs source code file of the Silverlight project.

  2. 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.
    ordersGrid.SelectionChanged +=
        new SelectionChangedEventHandler(ordersGrid_SelectionChanged);
    
    
    
  3. 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<T> that is bound to a second DataGrid.

    
            private void ordersGrid_SelectionChanged(object sender,
                SelectionChangedEventArgs e)
            {
                // Set the current order.
                currentOrder = (Order)ordersGrid.SelectedItem;
    
                // Clear the message text.
                messageTextBlock.Text = string.Empty;
    
                // Load details for the selected order.
                LoadDetails(currentOrder);
            }
    
            private void LoadDetails(Order order)
            {
                // Reset the order binding collection.
                detailsBindingCollection.Clear();
    
                if (order != null)
                {
                    try
                    {
                        // Begin the load related items.
                        svcContext.BeginLoadProperty(order, "Order_Details",
                            OnOrderDetailsLoaded, svcContext);
                    }
                    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(() => 
                    {
                        QueryOperationResponse response = null;
    
                        try
                        {
                            // Get the context from the result.
                            svcContext = result.AsyncState as NorthwindEntities;
    
                            // Get the end result of the load operation.
                            response = svcContext.EndLoadProperty(result);
    
                            // Enumeration executes the query.
                            foreach (Order_Detail item in response)
                            {
                                // Load the item into the binding collection.
                                detailsBindingCollection.Add(item);
                            }
    
                            // Get the continuation token from the response.
                            itemToken = response.GetContinuation<Order_Detail>(null);
    
                            if (itemToken != null)
                            {
                                // Get the next page if a continuation token was returned.
                                svcContext.BeginLoadProperty(currentOrder, "Order_Details", itemToken,
                                    OnOrderDetailsLoaded, svcContext);
                            }
                            else
                            {
                                // Reset and rebind the grid to the items binding collection 
                                // and update the layout.
                                this.detailsGrid.DataContext = detailsBindingCollection;
                                this.detailsGrid.UpdateLayout();
                                this.detailsGrid.Columns[0].Visibility = Visibility.Collapsed;
                                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

  1. From the Debug menu, select Start Debugging or Start Without Debugging.

    This builds and starts the application.

  2. 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.

  3. 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

Community Additions

ADD
Show:
© 2014 Microsoft