(X) Hide this
    • Login
    • Join
      • Say No Bots Generate New Image
        By clicking 'Register' you accept the terms of use .

Creating application with WCF RIA Services Part 5 – Sorting and Filtering data

(3 votes)
Martin Mihaylov
>
Martin Mihaylov
Joined Oct 29, 2007
Articles:   50
Comments:   70
More Articles
16 comments   /   posted on Apr 13, 2009
Categories:   Data Access , Line-of-Business
This article has been written for an older version of WCF RIA Services. To find out how to use the latest version of the WCF RIA Services read the dedicated article series and webinars by Brian Noyes.
WCF RIA Services Part 1: Getting Started | Watch the webinar recording

Introduction

In this previous article we added a DomainDataSource control in our application and bounded it to a DataGrid and with their help we easily visualized our data. Now we are going to implement some more goodies into the application - sorting and filtering. We achieve that via the DomainDataSource control, as it provides such functionality and is really simple to use.

Here is a link to the live demo at this stage and the source code. Note that they will be updated with each article! ;)

Sorting

The DomainDataSource has a collection property called SortDescriptors. We can set it to a collection of descriptors and the DomainDataSource will sort the data when it gets loaded. The SortDescriptor class can be found in the System.Windows.Data in the System.Windows.Ria.Controls assembly. So if there isn’t one, add a reference to that assembly and declare the namespace in your XAML:

<navigation:Page x:Class="WebAdministrationTool.ManageUsers"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 ...
                 xmlns:ria="clr-namespace:System.Windows.Controls;assembly=System.Windows.Ria.Controls"
                 xmlns:riaData="clr-namespace:System.Windows.Data;assembly=System.Windows.Ria.Controls"
                 ...
                 Title="ManageUsers Page">

After that add a SortDescriptor to the DomainDataSource control:

<ria:DomainDataSource x:Name="UsersDataSource" LoadMethodName="LoadAspnet_Users">
    <ria:DomainDataSource.SortDescriptors>
        <riaData:SortDescriptor Direction="Ascending" PropertyPath="UserName" />
    </ria:DomainDataSource.SortDescriptors>
    ...
</ria:DomainDataSource>

We want to sort the Data by UserName, so we set the propertyPath to that property’s name and choose the desired direction. I tried the following lines of code:

...
<ria:DomainDataSource.SortDescriptors>
    <riaData:SortDescriptorCollection>
        <riaData:SortDescriptor Direction="Ascending" PropertyPath="aspnet_Membership.IsApproved" />
        <riaData:SortDescriptor Direction="Ascending" PropertyPath="UserName" />
    </riaData:SortDescriptorCollection>
</ria:DomainDataSource.SortDescriptors>
...

with the idea to sort the users by the IsApproved flag first and after that by the name, but I got an error - “Unhandled Error in Silverlight 2 Application The value 'System.Windows.Data.SortDescriptorCollection' is not of type 'System.Windows.Data.SortDescriptor' and cannot be used in this generic collection”. Probably it will be available in the next releases or maybe there is another use of this SortDescriptorsCollection.

Filtering

The filtering allows us to take those items from data collection, which meet a certain requirement. Let’s say that we want to filter our data by the UserName. When typing in textbox, we want to be displayed those users whom UserName contains the typed string. In order to achieve this we use the FilterDescriptors property of the DomainDataSource control, but first let’s add a TextBox with the name “Filter”, that will allow the user to input the desired string. After that we add the FilterDescriptors:

<ria:DomainDataSource x:Name="UsersDataSource" LoadMethodName="LoadAspnet_Users">
    ...
    <ria:DomainDataSource.FilterDescriptors>
        <riaData:FilterDescriptorCollection>
            <riaData:FilterDescriptor PropertyPath="UserName" Operator="Contains">
                <riaData:ControlParameter ControlName="Filter" PropertyName="Text" RefreshEventName="TextChanged" />
            </riaData:FilterDescriptor>
        </riaData:FilterDescriptorCollection>
    </ria:DomainDataSource.FilterDescriptors>
    ...
</ria:DomainDataSource>

Like the SortDescriptor control, the FilterDescriptor is located in the System.Windows.Ria.Controls assembly. We set the PropertyPath to the property we are filtering by and the Operator, which determines the pattern of filtering. The last can have the following values:

  • Contains
  • EndsWith
  • IsContainedIn
  • IsEqualTo
  • IsGreaterThan
  • IsGreaterThanOrEqualTo
  • IsLessThan
  • IsLessThanOrEqualTo
  • IsNotEqualTo
  • StartsWith

The last step is to add a ControlParameter to the FilterDescriptor, in order to give the control a value to filter with. The ControlName is for the name of the control to use, the PropertyName points to the property that should be used as filter and RefreshEventName controls when data to be filtered and refreshed.

Note: In this case we can use the FilterDescriptionCollection and add two FilterDescriptors, which use the same TextBox – one to filter by UserName and one by Email.

Also note that when you filter a new load of the data is performed and the data is filtered. This is very useful as if we have a DataPager with PageSize 10 and a DomainDataSource with LoadSize 10, we are still able to filter the whole data and not just the loaded. However a call to the DomainService is made, so this means we have to wait for the data to load.

Conclusion

Except to load simple and easy data, the DomainDataSource is also able to manage it. It can perform sorting and filtering and they don’t take much time and effort to implement. As a conclusion about the DomainDataSource I can say that this is one great control that allows us to easily load data from the server, sort and filter it and after that to provide it to the UI controls. One is for sure – it saved me a lot of code!


Subscribe

Comments

  • -_-

    RE: Creating application with .NET RIA Services Part 5 – Sorting and Filtering data


    posted by Lmondria on Apr 23, 2009 07:46
    Hi,

    I try to filter a datagrid (\w data from a domaindatasource) using a textbox. I want the data to be filtered on two columns of the grid. is there a way to do this with the domaindatasource filterdescriptors, eg by adding two filterdescriptors for that textbox? I tried, but dont get any results.
  • -_-

    RE: Creating application with .NET RIA Services Part 5 – Sorting and Filtering data


    posted by Dharmender Kumar on Apr 28, 2009 10:22
    This is very good exercise for those who just start silverlight.
  • -_-

    RE: Creating application with .NET RIA Services Part 5 – Sorting and Filtering data


    posted by MichaelD! on Jun 12, 2009 16:05

    Martin:

    Use this for the SortDescriptors instead:

     

    <ria:DomainDataSource.SortDescriptors>
            <riaData:SortDescriptor Direction="Ascending" PropertyPath="aspnet_Membership.IsApproved" />
            <riaData:SortDescriptor Direction="Ascending" PropertyPath="UserName" />
    </ria:DomainDataSource.SortDescriptors>

    (your code sample is creating a collection and trying to set it to a read-only property.

  • -_-

    RE: Creating application with .NET RIA Services Part 5 – Sorting and Filtering data


    posted by Xavi on Jul 28, 2009 12:51
    The live demo seems not work with the relase version SL 3.

    Also, using the sources and compiling, I get many errors, probably because it is designed using SL beta. Please, can you update it to the release version?

     

    Thank you


  • -_-

    RE: Creating application with .NET RIA Services Part 5 – Sorting and Filtering data


    posted by greenlight on Aug 07, 2009 12:06
    Hello bro,

    I want to built a search form contains textbox, combobox, datetimepicker and a button Search. When I click this button, I will rebind my Grid. How to do it in code ?

    Please help me!

  • -_-

    RE: Creating application with .NET RIA Services Part 5 – Sorting and Filtering data


    posted by vijay on Sep 16, 2009 14:23
    Hi, thanks for nice article, i created one on similar lines, one of my database field is DateTime, my observation is when it stores the data in DB it stores correctly in local DataTime but when it is displayed using dataGrid it is dispayed in UTC format. Have you seen anything like this?

     Thanks,

    Vijay

  • -_-

    RE: Creating application with .NET RIA Services Part 5 – Sorting and Filtering data


    posted by Liem on Dec 16, 2009 11:44
    I am using System.Windows.Ria.dll version 1.0.41110.0 (the latest one so far) in Windows7.

    I could not find ControlParameter in namespace System.Windows.Data in assembly System.Windows.Control.Ria.dll, therefore i could not filtering. Please help me.

    Thanks.
  • -_-

    RE: Creating application with .NET RIA Services Part 5 – Sorting and Filtering data


    posted by JD on Dec 30, 2009 01:11

    Liem,

    Look in System.Windows.Controls in assembly System.Windows.Control.Ria.dll. I believe you may find it there.

  • -_-

    RE: Creating application with .NET RIA Services Part 5 – Sorting and Filtering data


    posted by raccoon on Jan 12, 2010 16:20

    Hi I have the same problem with Liem...

    this is the error that I found in my project

    Error 1 The tag 'ControlParameter' does not exist in XML namespace 'clr-namespace:System.Windows.Controls;assembly=System.Windows.Ria.Controls'. C:\Users\Silverlight3\Desktop\REVDemocode\WCFRIAServices\HRApp\HRApp\Views\EmployeeList.xaml 41 34 HRApp

  • -_-

    RE: Creating application with .NET RIA Services Part 5 – Sorting and Filtering data


    posted by raccoon on Jan 12, 2010 16:51
    Hi I already solve my problem it was my xaml namespace that type in as wrong name...
  • -_-

    RE: Creating application with .NET RIA Services Part 5 – Sorting and Filtering data


    posted by Alexander on Feb 18, 2010 21:23

    Sorting / Filtering / Grouping is really will be easy with using "RIA Services DataFilter Control for Silverlight".

    http://riadatafilter.codeplex.com/

  • -_-

    RE: Creating application with .NET RIA Services Part 5 – Sorting and Filtering data


    posted by Lingo on Apr 30, 2010 23:59

    1st, thanks for the example.

    questions: does the filter only accept string type parameter?

    if not, could you please give an example that use FilterOperator.IsContainedIn which parameter's value is an array of integer?

    thanks, 

  • -_-

    RE: Creating application with .NET RIA Services Part 5 – Sorting and Filtering data


    posted by Lingo on May 01, 2010 00:02

    1st, thanks for the example.

    questions: does the filter only accept string type parameter?

    if not, could you please give an example that use FilterOperator.IsContainedIn which parameter's value is an array of integer?

    thanks, 

  • les.prigmore

    RE: Creating application with .NET RIA Services Part 5 – Sorting and Filtering data


    posted by les.prigmore on Jun 29, 2010 04:15

    Use a Value Converter to convert the string to an integer.

    Here is a good example:

    http://www.silverlight.net/learn/videos/all/net-ria-services-intro/

    I hope that it helps.

  • les.prigmore

    RE: Creating application with .NET RIA Services Part 5 – Sorting and Filtering data


    posted by les.prigmore on Jun 29, 2010 04:18

    Filtering appeazrs to all have conjunctive "AND" if the a way to interpret and "OR" conjunctive?

     

    Thanks.........Les

  • Calabonga

    RE: Creating application with .NET RIA Services Part 5 – Sorting and Filtering data


    posted by Calabonga on Jul 05, 2010 04:59
    that's realy good thing? but what about FilterDescriptorCollection binding using MVVM pattern??

Add Comment

Login to comment:
  *      *       

From this series