P3P5
P3P5

Reputation: 1003

DataGrid sortng string with numbers in WPS c#

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

Answers (1)

Danellos
Danellos

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

Related Questions