Up Display code samples
DevForce 2010 Resource Center » Code samples » Display code samples » Code sample: Grouping and filtering with the ObjectDataSource (Silverlight)

Code sample: Grouping and filtering with the ObjectDataSource (Silverlight)

Last modified on August 15, 2012 17:21

This sample illustrates how to perform grouping and filtering using the DevForce ObjectDataSource (ODS) control. The ObjectDataSource allows you to define and configure a data source directly in your XAML. The ODS supports asynchronous querying, paging, grouping, sorting and filtering of an EntityQuery.


Problem

If you have a lot of data to display and need to allow users to page, group, sort and filter the data.  You prefer the simplicity of a drag and drop control.

Solution

The ODS allows you to define a data source directly in your XAML, which UI controls, such as the DataGrid and DataForm, can then bind to.  The ODS supports asynchronous querying, paging, grouping, sorting and filtering of an EntityQuery.

This sample shows you more advanced features of the ODS: how to define and use paging, sorting, grouping and filtering.

MainPage.xaml

The ObjectDataSource is located in the IdeaBlade.Windows namespace in the IdeaBlade.Windows.SL assembly, so you will need to add a reference to this assembly in your project, and a namespace declaration within the page markup.

XML
xmlns:ib="clr-namespace:IdeaBlade.Windows;assembly=IdeaBlade.Windows.SL" 

In the sample you'll see a DataGrid and DataPager, in addition the the ObjectDataSource control.  Note that the DataGrid's ItemSource and the DataPager's Source properties are both bound to the Data property of the ObjectDataSource.

Here's part of the markup for the ObjectDataSource control:

XML
<ib:ObjectDataSource x:Name="_customersDataSource"
  PageSize="20"                                 
  LoadSize="40"
  AutoLoad="True"
  QueryName="Customers"
  >
  <ib:ObjectDataSource.EntityManager>
    <em:NorthwindIBEntities
       EntityServerError="EM_EntityServerError
       Querying="
EM_Querying"
      >
    </em:NorthwindIBEntities>
  </ib:ObjectDataSource.EntityManager>
 …

The PageSize indicates the number of items to be displayed on a page.

The LoadSize indicates the number of items to be loaded upon each page request. Setting the LoadSize to a value larger than the PageSize allows for “look ahead” caching of pages.

AutoLoad indicates whether the control will automatically execute the provided query once the control initializes or wait until the Load() method is called from code behind.

QueryName is the name of a property or method which will return the IEntityQuery serving as the data source.  Since we didn't include a TypeName property indicating on which type the QueryName can be found, it's assumed to be on the EntityManager.  In this sample, we're using the "Customers" property defined on the NorthwindIBEntities generated by the NorthwindIB entity model.

The EntityManager property indicates the EntityManager to be used.  Since included here in markup, a new "NorthwindIBEntities" will be constructed.  If you don't want a new EM, you have two options:

  1) set the UseDefaultManager property to "True" to use the current DefaultManager, or

  2) set the EntityManager property in code behind to the EM you wish to use

Note that we’ve also provided a few event handlers on the EntityManager to show what's happening as you run the sample.

Sorting, Grouping and Filtering

Next we see the various descriptors which can be included to customize the grid display.

XML
… continued

  <ib:ObjectDataSource.SortDescriptors>
    <ib:SortDescriptor PropertyPath="CompanyName" Direction="Ascending" />
  </ib:ObjectDataSource.SortDescriptors>

  <ib:ObjectDataSource.GroupDescriptors>
    <ib:GroupDescriptor PropertyPath="Country" />
  </ib:ObjectDataSource.GroupDescriptors>

  <ib:ObjectDataSource.FilterDescriptors>
   <ib:FilterDescriptor PropertyPath="ContactName" Operator="StartsWith">
          <ib:ControlParameter PropertyName="Text"
             RefreshEventName="TextChanged"
             ControlName="_contactFilterBox" />
   </ib:FilterDescriptor>
  </ib:ObjectDataSource.FilterDescriptors>
</ib:ObjectDataSource>

Sorting is provided using one or more SortDescriptor items. A SortDescriptor defines the PropertyPath and sort direction. The PropertyPath is a Parameter (described below) which specifies the property on the displayed element which will be used to sort the results.

Grouping allows you to display the results in a hierarchy, and is specified using one or more GroupDescriptor items. The PropertyPath is a Parameter which specifies the property on the element which will be used to group the data.

Filtering allows your user to dynamically filter the data displayed. Use one or more FilterDescriptor items to allow filtering. The FilterDescriptor defines a PropertyPath, operator and filter value. The filter Value is a Parameter.

Parameters allow you to substitute a ControlParameter for a string representation and point to a UI control on the page which will provide the run time value. Above we see a ControlParameter used to define the FilterDescriptor’s filtering Value: indicating that the Text property of the _contactFilterBox control will provide the value whenever the TextChanged event fires. Although not shown above, the PropertyPath of both the SortDescriptor and GroupDescriptor can also be specified via a ControlParameter.

Prerequisites

The Silverlight 4 Toolkit

Created by DevForce on October 24, 2010 11:32

This wiki is licensed under a Creative Commons 2.0 license. XWiki Enterprise 3.2 - Documentation. Copyright © 2015 IdeaBlade