Home > Enterprise >  WPF DataGrid sorting data of type "double"
WPF DataGrid sorting data of type "double"

Time:01-09

I have run into a problem

that I want to sort a "double" value in the DataGrid, these values are known to be stored as "string".

When I press sort (on the header), then it sorts the data from the column "money total" as type "string".

(see here)

The "moneyTotal" column

Thats my DataGridTextColumn (unfortunately there is no DataGridDoubleColumn)

<DataGridTextColumn Header="{DynamicResource moneyTotal}" Binding="{Binding StringMoneyTotal}"/>

Thats my Model (have removed the unimportant code)

public class InvoiceModel
{
    public double MoneyTotal { get; set; }
    public string StringMoneyTotal { get { return String.Format("{0:0.0,0}", MoneyTotal); }} 
}

Is there a solution how I can really sort by double? unfortunately the current sorting is useless

CodePudding user response:

Two ways to fix this:

  1. As suggested by Andy in the comments, just bind to MoneyTotal (no need for StringMoneyTotal) and provide the formatting separately, e.g.: Binding="{Binding MoneyTotal, StringFormat={}{0:0.00}}". Alternatively to StringFormatyou could also use a Converter.

  2. Keep the binding as is, but specify by which property to sort: SortMemberPath="MoneyTotal"

  • Related