Master-Detail Binding

As we've already seen in the Server-Side Filtering example, C1DataSource supports master-detail binding. With very large datasets, server-side filtering is by far the best solution, but with smaller datasets, client-side filtering can be just as efficient. This next scenario demonstrates client-side master-detail binding using a grid, instead of a combo box like in our previous example, to select our categories.

To implement master-detail binding, follow these steps:

1.   Using the project we created to demonstrate Server-Side Filtering, add a new form with a C1DataSource component using the same DomainContextTypeName as before and create a view source based on GetCategories. Note that you can make this the startup form to save time when you run the project.

2.   Next, add the master grid to the form and bind it to GetCategories:

 

ItemsSource="{Binding [GetCategories], ElementName=c1DataSource1}"

 

These are the same steps we followed earlier in the simple binding scenario.

3.   Now, add the detail grid to the form and bind it to the Products of the category currently selected in the master grid. This can be done using the following binding in XAML:

 

ItemsSource="{Binding SelectedItem.Products, ElementName=dataGrid1}"

 

There is one more thing needed to make it work: tell RIA Services to include all products belonging to a category with categories when it fetches data using the GetCategories method of the domain service. If we don’t do this, GetCategories will fetch only categories data, without their products, so our bottom grid will be empty.

4.   Manually modify the domain service (default name DomainService1.cs) and metadata (default name DomainService1.metadata.cs) files generated by Visual Studio when you added the Domain Service class to the Web project. In DomainService1.cs,add the Include operator to the query in the GetCategories method:

      Visual Basic

      C#

5.   In the DomainService1.metadata.cs. add the [Include] attribute before the corresponding property:

      Visual Basic

      C#

6.   Save, build and run the application. Select a category in the first grid and notice the products that belong to that category on the bottom grid. You can still edit the categories and products exactly as before.


Send us comments about this topic.
Copyright © GrapeCity, inc. All rights reserved.