Using the Infragistics XamGrid and XamDataChart with Hierarchical Data and WCF RIA Services - Part1

[Infragistics] Mihail Mateev / Thursday, June 17, 2010

This Article describes how to use the Infragistics XamGrid and XamDataChart with WCF RIA Services.

Some of the most important things, related with WCF RIA Services and using the Infragistics XamGrid and RIA Services are described in the article "Using the Infragistics XamGrid with RIA Services".

One more interesting topic is using the hierarchical data with WCF RIA Services.

Part 1 of the article describes using the Infragistics XamGrid with hierarchical data.

To create an application you need to repeat first six steps from the previous article: "Using the Infragistics XamGrid with RIA Services":

Steps to create a RIA Services enabled Silverlight application:

  • 1. Download the trial version of NetAdvantage for Silverlight LoB 10.2
  • 2. Install WCF RIA Services for Silverlight 4 and Visual Studio 2010 (it includes also WCF Ria Services)
  • 3. Create a Silverlight application, hosted in an ASP.Net application with RIA Services link
  • 4. Create Data Model
  • 5. Create a Domain Service
  • 6. Create a Silverlight Client

You need to install trial version of NetAdvantage for Silverlight LoB 10.2

Data Connection used for this application use the NorthWind  sample database. You could download it from there

 ADO.Net Entity Data Model:

ADO.Net Entity Data Model is created from the NorthWind database and is named  NorthWindModel  (saved like  NorthWindModel.admx).

Tables Customer and Order are included in this Entity Data Model.

Domain Services Class is created with the name NorthWindDomainService.

 

Create the Silverlight Client

Data Sources, presented classes Customer and Order from our Entity Data Model are available

 

When XamGrid , bound to the Customer data is created from Data source c is no hierarchical data represented:

By default data from the child table is not included

 Let's look at our code:

 

<riaControls:DomainDataSource AutoLoad="True" d:DesignData="{d:DesignInstance my:Customer, CreateList=true}" Height="0" LoadedData="CustomerDomainDataSourceLoadedData" Name="customerDomainDataSource" QueryName="GetCustomersQuery" Width="0    <riaControls:DomainDataSource.DomainContext>
        <my:NorthWindDomainContext />
    </riaControls:DomainDataSource.DomainContext>
</riaControls:DomainDataSource>
 <ig:XamGrid Grid.Row="1" Grid.Column="1"  AutoGenerateColumns="False" HorizontalAlignment="Left" ItemsSource="{Binding ElementName=customerDomainDataSource, Path=Data}" Margin="20" Name="customerXamGrid" VerticalAlignment="Top" />

 

Method GetCustomers() is declared in NortWindDomainService class:

 

 public IQueryable<Customer> GetCustomers()

{
    return this.ObjectContext.Customers;
}

 

To receive Orders

In the CustomerMetadata class there added Orders with Include attribute.

 

 [Include]

public EntityCollection<Order> Orders { get; set; }

 

In the NortWindDomainService class add method GetCustomersWithOrders():

 

 public IQueryable<Customer> GetCustomersWithOrders()

{
    IQueryable<Customer> customers = this.ObjectContext.Customers.Include("Orders");
    return customers;
}

 

In the customerDomainDataSource DomainDataSource change the QueryName from GetCustomersQuery to GetCustomersWithOrdersQuery:

 

<riaControls:DomainDataSource AutoLoad="True" d:DesignData="{d:DesignInstance my:Customer, CreateList=true}" Height="0" LoadedData="CustomerDomainDataSourceLoadedData" Name="customerDomainDataSource" QueryName="GetCustomersWithOrdersQuery" Width="0">
    <riaControls:DomainDataSource.DomainContext>
        <my:NorthWindDomainContext />
    </riaControls:DomainDataSource.DomainContext>
</riaControls:DomainDataSource>

 

Now hierarchical data is displayed on the XamGrid.

 

 Create summary fields:

In the NortWindModel.Designer.cs add a Freight property the Customer class:

  public Decimal Freight { get; set;}

In the CustomerMetadata class (NortWindDomainService.metadata.cs) add:

 public decimal Freight { get; set; }
In the NortWindDomainService.cs add:
 public decimal GetCustomersFreight(Customer customer)
{
    return Enumerable.Sum(orders.Where(o => o.Freight != null && o.CustomerID == customer.CustomerID), o => (decimal)o.Freight);
}
 Modify GetCustomersWithOrders() method:
public IQueryable<Customer> GetCustomersWithOrders()
{
    IQueryable<Customer> customers = this.ObjectContext.Customers.Include("Orders");
    foreach(Customer customer in customers)
    {
        customer.Freight = GetCustomersFreight(customer);
    }
    return customers;
}

 

Now Total of Freight is calculated

 

In the part 2 of this article will be demonstrated the new Infragistics XamDataChart , used with WCF RIA Services.