I am working with a DataGrid
in WPF with several decimal
columns. I recently changed the bound properties to decimal?
s and now these columns are not able to be sorted by clicking on the header (as my other columns are). However, I can still sort them using methods in my code-behind. Does anybody know if it's possible to sort the decimal?
columns by clicking the header or what the reason behind it is if it's not possible? Here is my relevant code:
A couple of the columns in question (The attribute attaches to a behavior for the DataGrid. Enabling/disabling the behavior makes no difference):
[Column("PPAvg", 7)]
public decimal? ProjectedPointsAvg { get; set; }
[Column("PPHi", 8)]
public decimal? ProjectedPointsHi { get; set; }
[Column("PPLo", 9)]
public decimal? ProjectedPointsLo { get; set; }
The DataGrid itself:
<DataGrid x:Name="poolDataGrid"
Grid.Row="1"
CanUserAddRows="False"
IsReadOnly="True"
MouseDoubleClick="poolDataGrid_MouseDoubleClick">
<i:Interaction.Behaviors>
<local:ColumnHeaderBehavior />
</i:Interaction.Behaviors>
</DataGrid>
I can still sort via code-behind using something like this:
var col = poolDataGrid.Columns.SingleOrDefault(c => c is DataGridTextColumn && c.Header.ToString() == "PPHi");
poolDataGrid.Items.SortDescriptions.Add(new SortDescription(col.SortMemberPath, ListSortDirection.Descending));
Unfortunately, I don't have much else to add. If anybody could help me out with this I would greatly appreciate it.
You can sort a DataGrid with nullable property but you need to define all columns on your own. So it should be something like this:
<DataGrid Name="poolDataGrid" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn Header="Column1" Binding="{Binding ProjectedPointsAvg }"/>
<DataGridTextColumn Header="Column2" Binding="{Binding ProjectedPointsHi }" />
<DataGridTextColumn Header="Column3" Binding="{Binding ProjectedPointsLo}" />
</DataGrid.Columns>
</DataGrid>
Why does AutoGenerateColumns
have problems with nullalble properties?
It appears that columns are only auto-generated for properties whose type is a bindable type.
More: http://vaultofthoughts.net/IsBindableTypeMysteryMethod.aspx
Tested on: VS2012 .NET 4.5