Reputation: 65
I'm trying to get my column to sort using the datagrid SortMemberPath="COLUMN"
attribute and I get a weird sort order like so:
It looks like the column is sorted on the basis of the first digit of the number and not the entire number. How can I fix this?
Edit:
Okay, I see what the problem is. I'm using this converter now but still no change.
public class IntToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is string)
{
string s = (string)value;
int num;
if (int.TryParse(s, out num))
return Int32.Parse(s);
}
return DependencyProperty.UnsetValue;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
int number = (int)value;
return number.ToString();
}
}
I added this to my xaml file Windows.Resource:
<me:IntToStringConverter x:Key="IntToStringConverter"/>
and made this change in my data part:
<toolkit:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding MinRun, Converter={StaticResource IntToStringConverter}}" HorizontalAlignment="Center" />
</DataTemplate>
</toolkit:DataGridTemplateColumn.CellTemplate>
Here is the DataGrid part of the XAML. Note that out of all the columns, I'm first trying to get the 'MinRun' column to sort properly first, then I'll use the same technique on the other columns.
Upvotes: 3
Views: 4208
Reputation: 6963
Given
A DataGrid showing a collection of TestResults where elapsed time was converted to string in the model. When bound to the DataGrid sorting was by string and not numeric value.
Solution shown below: The var qry
was a List
of a custom-type with the property TestElapsedTime
of type Double
.
Add a Sorting Event Handler to DataGrid
<DataGrid x:Name="testResultsDataGrid" Sorting="OnSorting">
In the EventHandler
private void OnSorting(object sender, DataGridSortingEventArgs e)
{
var col = e.Column.Header.ToString();
if (col == "Elapsed Time")
{
var qry = GetCollectionViewSourceDefaultView();
qry.Sort((x, y) =>
{
var diff = x.TestElapsedTime - y.TestElapsedTime;
return (int)diff;
});
}
}
Problems
The elapsed time was a double so the lambda had to perform a cast to int as shown.
Surprises
The direction of the sort is determined by the displayed datagrid, it knows how to use the one sort function to sort both directions!
Upvotes: 0
Reputation: 1
Luckly for me i could change the original class, but I'm interested in the answer.
I added a readonly integer property.
Upvotes: -2
Reputation: 1532
Looks like it's doing a string sort, which suggests to me that you're binding that column to a String property. Change the property to be an integer type, and DataGrid will sort correctly.
In my experience, programmers make the property a string type because they want to, for example, to display a blank string when the value is invalid. Use a ValueConverter for that instead, so you have the flexibility of displaying any string you want while WPF does a numeric sort on the underlying data.
Upvotes: 2
Reputation: 1398
Your column is probably bound to a String
object.
Therefore, the sort do an alphabetic job, not a numeric.
You should bind your column to int
values.
Upvotes: 2