Reputation: 1003
I have a problem sorting my data in wpf datagrig. I have ID column that takes strings, for example: E1, E2, E10, E3, E9, E4, E5 I want it to be sorter like this: E1, E2, E3, E4, E5, E9, E10 But instead it sorts it like this: E1, E10, E2, E3, E4, E5, E9
This is my code that I use:
<DataGridTemplateColumn SortMemberPath="ID" CanUserSort="true" SortDirection="Ascending" Header="IDs" Width="Auto" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox Width="Auto" IsReadOnly="True" Text="{Binding Path=UserID}"></TextBox>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
If someone could help me with the code that would be great. Thank you.
Upvotes: 0
Views: 123
Reputation: 31
It is sorting that way because its sorting it as a string, which sorts the values alphabetically.
If you absolutely have to have the "E" (or whatever else) with the number, try to implement your own custom sorting rule, as explained here:
https://stackoverflow.com/a/2130557/1758369
Obviously your algorithm will be different, but that's the general idea.
Upvotes: 2