<datagrid:GridTemplateColumn
HeaderText="{shared:Translate Bulls}"
MappingName="ShortName"
MinimumWidth="140"
ColumnSizer="Auto"
AllowSorting="True"
HeaderCellTextSize="{StaticResource SmallCaptionFontSize}"
CellTemplate="{StaticResource BullListTemplateSmall}" />
</datagrid:SfDataGrid.Columns>
Template being used:<DataTemplate
x:Key="BullListTemplateSmall">
<ViewCell>
<Grid
ColumnSpacing="0"
HorizontalOptions="FillAndExpand"
VerticalOptions="StartAndExpand">
<!--BackgroundColor="{StaticResource AppColorWhite}"-->
<Grid.ColumnDefinitions>
<ColumnDefinition
Width="Auto" />
<ColumnDefinition
Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition
Height="Auto" />
<RowDefinition
Height="*" />
</Grid.RowDefinitions>
<Label
Text="{Binding ShortName}"
Style="{StaticResource LabelSubheadingStyle}"
VerticalTextAlignment="Center"
HorizontalOptions="FillAndExpand"
FontSize="{StaticResource SubheadingFontSize}"
FontAttributes="Bold"
Margin="10,5,5,0"
Grid.Column="0"
Grid.ColumnSpan="2"
Grid.Row="0" />
<StackLayout
Grid.Row="1"
VerticalOptions="Center"
Margin="0,0,0,10">
<StackLayout
Spacing="0"
Grid.Row="1"
VerticalOptions="Center"
HorizontalOptions="Start">
<StackLayout
Orientation="Horizontal"
Margin="10, 0, 0, 0"
VerticalOptions="Center">
<Label
Text="{shared:Translate BullCode}"
Style="{StaticResource LabelSubheadingStyle}"
TextColor="{StaticResource AppColorMediumGray}"
FontSize="{StaticResource CaptionFontSize}"
FontAttributes="Bold"
VerticalTextAlignment="Center" />
<Label
Text="{Binding BullCode}"
Style="{StaticResource LabelSubheadingStyle}"
FontSize="{StaticResource CaptionFontSize}"
VerticalTextAlignment="Center" />
</StackLayout>
<StackLayout
Orientation="Horizontal"
Margin="10, 0, 0, 0"
VerticalOptions="Center">
<Label
Text="{shared:Translate RegNo}"
Style="{StaticResource LabelSubheadingStyle}"
TextColor="{StaticResource AppColorMediumGray}"
FontSize="{StaticResource CaptionFontSize}"
FontAttributes="Bold"
VerticalTextAlignment="Center" />
<Label
Text="{Binding RegistrationNumberWithoutLeadingZeros}"
Style="{StaticResource LabelSubheadingStyle}"
FontSize="{StaticResource CaptionFontSize}"
VerticalTextAlignment="Center" />
</StackLayout>
</StackLayout>
</StackLayout>
</Grid>
</ViewCell>
</DataTemplate>
//Button Click code decided Mapping name.
private void Button_Clicked(object sender, EventArgs e)
{
dataGrid.SortComparers.Clear();
if (changebtn.Text == "OrderID")
{
dataGrid.SortComparers.Add(new SortComparer()
{
PropertyName = "ShipCountry",
Comparer = new CustomComparer("ShortName")
});
changebtn.Text = "ShortName";
}
else
{
dataGrid.SortComparers.Add(new SortComparer()
{
PropertyName = "ShipCountry",
Comparer = new CustomComparer("OrderID")
});
changebtn.Text = "OrderID";
}
} |
public class CustomComparer : IComparer<Object>, ISortDirection
{
public string MappingName;
public CustomComparer(string mappingName)
{
MappingName = mappingName;
}
public int Compare(object x, object y)
{
//For OrderInfo type data
if (MappingName == "OrderID")
{
int nameX = ((OrderInfo)x).OrderID;
int nameY = ((OrderInfo)y).OrderID;
// Objects are compared and return the SortDirection
if ((int)nameX.CompareTo((int)nameY) > 0)
return SortDirection == ListSortDirection.Descending ? 1 : -1;
else if (nameX.CompareTo(nameY) == -1)
return SortDirection == ListSortDirection.Descending ? -1 : 1;
else
return 0;
}
else if (MappingName == "ShortName")
{
string nameX= ((OrderInfo) x).ShortName;
string nameY = ((OrderInfo)y).ShortName;
// Objects are compared and return the SortDirection
if (nameX.CompareTo(nameY) > 0)
return SortDirection == ListSortDirection.Descending ? 1 : -1;
else if (nameX.CompareTo(nameY) == -1)
return SortDirection == ListSortDirection.Descending ? -1 : 1;
else
return 0;
}
else
{
return 0;
}
}
//Gets or sets the SortDirection value
public ListSortDirection SortDirection { get; set; }
}
|
public void ChangeSortButton_Clicked()
{
sfGrid.SortComparers.Clear();
if (changeButtonLabel.Text == TranslationHelper.Translate("BullCode"))
{
sfGrid.SortComparers.Add(new SortComparer()
{
PropertyName = "ShortName",
Comparer = new CustomComparer("ShortName")
});
changeButtonLabel.Text = TranslationHelper.Translate("ShortName");
}
else
{
sfGrid.SortComparers.Add(new SortComparer()
{
PropertyName = "ShortName",
Comparer = new CustomComparer("BullCode")
});
changeButtonLabel.Text = TranslationHelper.Translate("BullCode");
}
SortGrid();
}
private void SortGrid()
{
sfGrid.SortColumnDescriptions.Add(new SortColumnDescription()
{
ColumnName = "ShortName",
SortDirection = ListSortDirection.Ascending
});
}