Casuzen
Casuzen

Reputation: 169

How to sort data from XML as numeric in a dataGrid

I have data in an XML file that I display in a dataGrid. (WPF, C#, VisualStudio 2013) One of the columns is numeric data. When I sort the column, it seems like it is translating it as alpha instead of numeric, though I have tried to specify that column as numeric...

XAML for dataGrid:

<DataGrid 
    ItemsSource="{Binding Path=Elements[Record]}"
    AutoGenerateColumns="False" Height="Auto" 
    Name="dataGridRank" 
    VerticalAlignment="Top" HorizontalAlignment="Stretch">

    <DataGrid.Columns>
        <DataGridTextColumn 
           Header="Name" 
           Width="*"
           Binding="{Binding Element[Name].Value}"/>
        <DataGridTextColumn 
           Header="Rank" 
           Width="*"
           Binding="{Binding Element[Rank].Value, StringFormat={}{0:N}}"/>
    </DataGrid.Columns>

</DataGrid>

XMLfile 'ranks.xml':

<Ranks xmlns:dt="urn:schemas-microsoft-com:datatypes">
  <Record>
     <Name> A </Name>
     <Rank dt:dt="number"> 85 </Rank>
  </Record>
  <Record> 
    <Name> C </Name>
    <Rank dt:dt="number"> 105 </Rank>
  </Record>
  <Record>
    <Name> B </Name>
    <Rank dt:dt="number"> 95</Rank>
  </Record>
</Ranks>

C# code to load the xml file:

var xml = XDocument.Load("Resources/ranks.xml").Root;
dataGridRanks.DataContext = xml;

What I want when I click on the column header to sort Rank is:

A 85

B 95

C 105

or 

C 105

B 95

A 85

And what I get:

C 105

A 85

B 95

or 

B 95

A 85

C 105

I have unsuccessfully tried both with and without the Stringformat or the dt:dt="number". I have tried Stringformat=N as well as StringFormat={}{0:N}.

What am I missing? It seems like this should be simple...

Upvotes: 2

Views: 706

Answers (1)

Rohit Vats
Rohit Vats

Reputation: 81243

Since Element value attribute is of type string. So column type is of string hence default comparer of string is getting used here.

And StringFormat won't work here since it won't change the type of binded property. It's just used for formatting string on GUI.

I would suggest to create anonymous type at runtime and bind it with that where your Rank property will of be of type int so that default comparer of int gets used.

Code:

var xml = XDocument.Load("database.xml").Root;
dataGridRank.ItemsSource = xml.Elements("Record")
                            .Select(e => new { Name = e.Element("Name").Value,
                             Rank = Convert.ToInt32(e.Element("Rank").Value) });

XAML:

    <DataGrid AutoGenerateColumns="False" Height="Auto" 
              Name="dataGridRank" 
              VerticalAlignment="Top" HorizontalAlignment="Stretch">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Name" 
                                Width="*"
                                Binding="{Binding Name}"/>
            <DataGridTextColumn Header="Rank" 
                                Width="*"
                                Binding="{Binding Rank}"/>
        </DataGrid.Columns>

    </DataGrid>

Upvotes: 2

Related Questions