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)
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:
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 toStringFormat
you could also use aConverter
.Keep the binding as is, but specify by which property to sort:
SortMemberPath="MoneyTotal"