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

This post has 5 Replies | 3 Followers

daraanna
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" >

<igDP:Field.Settings>

<igDP:FieldSettings

AllowEdit="False"

CellValuePresenterStyle="{StaticResource CurrentStyle}"

CellMaxHeight="20"/>

</igDP:Field.Settings>

</igDP:Field>

 

//-----------------------------------------------------------------------------------------------------

 

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.");

}

#endregion

}

 

//---------------------------------------------------------

 

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

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

<Setter Property="Template">

<Setter.Value>

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

<TextBlock

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

VerticalAlignment="Center"

Foreground="#FF151C55"

Margin="5,0,0,0"/>

</ControlTemplate>

</Setter.Value> </Setter>

</Style>

 

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
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 http://forums.infragistics.com/forums/p/5500/24571.aspx#24571) 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)

Joe

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

 Thank you very much :)

That's just what we need

  • Post Points: 5
vinodsardar
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()

xamDataGrid.SortComparer=comp

 

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.

 

Regards,

Vinod Sa.

 

 

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

 

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

if (fieldDATE != null)

     //sort

     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
vinodsardar
Points 80
Replied On: Mon, Dec 1 2008 12:52 AM

 Hi,

 

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.

 

 

 Thanks,

Vinod Sa.

 

 

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