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 Memberspublic 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.
2. Datatable1 of this dataView contains column "Id"
<igDP:Field.Settings>
AllowEdit="False"
CellMaxHeight="20"/>
</igDP:Field>
class CurrentStyleConverter : IValueConverter
{
}
DataRecord dr = value as DataRecord;
#endregion
<Style x:Key="CurrentStyle" TargetType="{x:Type igDP:CellValuePresenter}">
<Setter.Value>
<TextBlock
Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Value, Converter={StaticResource CurrentStyleConverter}}"
VerticalAlignment="Center"
Foreground="#FF151C55"
</ControlTemplate>
</Style>
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.
Field fieldDATE = xamDataGrid1.FieldLayouts[0].Fields["DateActivity"]; if (fieldDATE != null) //sort fieldDATE .Settings.SortComparer = new EditDatesActivityComparer(); //fieldDATE .Settings.GroupByComparer = new EditDatesActivityComparer();
if (fieldDATE != null)
//sort
fieldDATE .Settings.SortComparer = new EditDatesActivityComparer();
//fieldDATE .Settings.GroupByComparer = new EditDatesActivityComparer();
Hi I hae written following comparer class :
Implements IComparer
End Sub
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
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,
Thank you very much :)
That's just what we need
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