Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
660
How to sort grid by a certain field by custom sorting condition?
posted
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.