How to sort grid by a certain field by custom sorting condition?

This post has 5 Replies | 3 Followers

Points 660
Replied On: Wed, Apr 2 2008 10:19 AM
How to sort grid by a certain field using my own custom sorting condition? 


For example I have:


1.  One DataView dv, that is binding for FieldLayout[0] of XamDataGrid grid

2.  Datatable1 of this dataView contains column "Id"

3.  Column "Id" is a foreign key for datatable1 and primary key of datatable2 4.  With converter's help I set "Name" instead "Id" in grid                


<igDP:Field Name="Id" Label="Name" >




CellValuePresenterStyle="{StaticResource CurrentStyle}"







class CurrentStyleConverter : IValueConverter


#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)


if (value != null && (value is int || value is string))


return DataSet1.GetNameById((int)value);


if (value != null && value is DataRecord)


DataRecord dr = value as DataRecord;

if (dr != null && dr.DataItem != null && dr.DataItem is DataRow)


DataSet1.DataRow1 row = dr.DataItem
as DataSet1.DataRow1;return DataSet1.GetNameById(row.Id);



return value;


public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)


throw new Exception("The method or operation is not implemented.");







<local:CurrentStyleConverter x:Key="CurrentStyleConverter"/>

<Style x:Key="CurrentStyle" TargetType="{x:Type igDP:CellValuePresenter}">

<Setter Property="Template">


<ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}">


Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Value, Converter={StaticResource CurrentStyleConverter}}"





</Setter.Value> </Setter>



As a result: sorting "by default" by "Id" column in grid:  is by "Id" field


I'd like, that sorting by "Id" column would make the grid sorted by Name assigned to each Id.


How can I easily do this? 


Thanks for help.
  • Post Points: 35
[Infragistics] JoeM
Points 8,576
Infragistics Employee
Replied On: Thu, Apr 3 2008 12:58 PM

Hi -

To sort the Id column by Name using your custom CellValuePresenter style with your converter, you would have to specify a custom SortComparer (i.e., an object that implements the IComparer interface) via the 'Id' field's Field.Settings.SortComparer property.

If you are using the latest hotfix (see this post then you can implement a simpler solution that eliminates the custom CellValuePresenter style and SortComparer.  We have added a Converter property to the Field object.  Simply set your converter there and the converted value will be used when the column is sorted (don't forget to remove your CellValuePresenter style and SortComparer - they are not needed for this solution)


  • Post Points: 20
Points 660
Replied On: Fri, Apr 4 2008 4:29 AM

 Thank you very much :)

That's just what we need

  • Post Points: 5
Points 80
Replied On: Wed, Nov 26 2008 9:45 AM

Hi I hae written following comparer class :

Public Class EditDatesActivityComparer

Implements IComparer

Public Sub New()

End Sub

Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare

Return String.Compare(DirectCast(x, EditDatesActivity).ParentCaption, DirectCast(y, EditDatesActivity).ParentCaption)

End Function

End Class


and set SortComparer Property of XamDataGrid as follows

Dim comp as New EditDatesActivityComparer()



but still Compare method is not getting called.but New method is getting called when object is created.


Please let me know what would be the issue or I am doing anything wrong.



Vinod Sa.



  • Post Points: 20
Points 660
Replied On: Fri, Nov 28 2008 9:19 AM


Field fieldDATE = xamDataGrid1.FieldLayouts[0].Fields["DateActivity"];

if (fieldDATE != null)


     fieldDATE .Settings.SortComparer = new EditDatesActivityComparer();

    //fieldDATE .Settings.GroupByComparer = new EditDatesActivityComparer();


  • I think problem is in that you set comparer for grid. Better is if you set Comparer for Field, which  you try to sort :)
  • Post Points: 20
Points 80
Replied On: Mon, Dec 1 2008 12:52 AM



Thankls for your reply. Problem was I was doing group by in the XamDataGrid and was using SortComparer Property.because of this Compare method was not called by default instead it gets called when you click on column label.


What I found is whenever you are using group by in XamDataGrid and want to use custom sorting then use tag called GroupByComparer as you mentioned.




Vinod Sa.



  • Post Points: 5
Page 1 of 1 (6 items) | RSS