【问题标题】:Wpf DataGrid (German) Date SortWpf DataGrid(德语)日期排序
【发布时间】:2017-07-11 08:30:42
【问题描述】:

我正在寻找一种(最好仅适用于 XAML)解决方案,通过以dd.MM.yyyy 格式显示日期值的列对 WPF DataGrid 进行自动排序。 DataGrids ItemSource 是一个 DataTable,它是从 XML 字符串中读取的。

我已经拥有的代码似乎不起作用,但是,如果我设置例如SortDirection="Ascending" 在显示整数的列上,它确实有效。我省略了与我的问题无关的所有代码。

希望你能帮助我,在此先感谢。

<DataGrid IsReadOnly="True"
          ItemsSource="{Binding Path=MyDataTable}"
          AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Date" Binding="{Binding Date, StringFormat='{}{0:dd.MM.yyyy}'}" SortDirection="Ascending"/>
    </DataGrid.Columns>
</DataGrid> 

编辑:

DataTable 由方法 DataTable.ReadFromXml() 从以下字符串反序列化:

<DataTable
    xmlns=\"http://schemas.datacontract.org/2004/07/System.Data\">
    <xs:schema id=\"NewDataSet\"
        xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"
        xmlns=\"\"
        xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\">
        <xs:element name=\"NewDataSet\" msdata:IsDataSet=\"true\" msdata:MainDataTable=\"ResultTable\" msdata:UseCurrentLocale=\"true\">
            <xs:complexType>
                <xs:choice minOccurs=\"0\" maxOccurs=\"unbounded\">
                    <xs:element name=\"ResultTable\">
                        <xs:complexType>
                            <xs:sequence>
                                <xs:element name=\"Date\" type=\"xs:dateTime\" minOccurs=\"0\"/>
                            </xs:sequence>
                        </xs:complexType>
                    </xs:element>
                </xs:choice>
            </xs:complexType>
        </xs:element>
    </xs:schema>
    <diffgr:diffgram
        xmlns:diffgr=\"urn:schemas-microsoft-com:xml-diffgram-v1\"
        xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\">
        <DocumentElement
            xmlns=\"\">
            <ResultTable diffgr:id=\"ResultTable1\" msdata:rowOrder=\"0\">
                <Date>2017-03-11T00:00:00+01:00</Date>
            </ResultTable>
            <ResultTable diffgr:id=\"ResultTable2\" msdata:rowOrder=\"0\">
                <Date>2017-22-11T00:00:00+01:00</Date>
            </ResultTable>
            <ResultTable diffgr:id=\"ResultTable3\" msdata:rowOrder=\"0\">
                <Date>2017-03-11T00:00:00+01:00</Date>
            </ResultTable>
        </DocumentElement>
    </diffgr:diffgram>
</DataTable>

编辑#2:

我现在不是试图找到一个(显然相当困难的)Xaml-Only 解决方案,而是直接在 MySQL 中从我获取它的地方对 DataTable 进行排序。这被证明是一种更简单、更快速的解决方案。

【问题讨论】:

  • 用户是否需要能够通过单击列来对列进行排序,或者您可以在后面的代码中进行排序吗?
  • 经过更多研究,我认为这可能是因为绑定中的字符串格式导致它作为结果字符串排序。您可以尝试使用格式 yyyy-MM-dd 看看是否有效。
  • Date 是日期时间还是字符串? IIRC,DateTime 列默认按底层 DateTime 排序,而不是按显示的数据排序。
  • @apc 感谢您的回答 :) 用户也应该能够进行排序,如果他单击标题,排序似乎也可以正常工作..
  • @Kilazur Date 是 DataTable 中的 DateTime

标签: c# wpf sorting date datagrid


【解决方案1】:

您可以绑定到CollectionViewSource,而不是绑定到DataView,默认情况下该Date 属性对视图进行排序:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
        mc:Ignorable="d"
        Title="MainWindow" Height="300" Width="300">
    <Window.Resources>
        <CollectionViewSource x:Key="cvs" Source="{Binding MyDataTable}">
            <CollectionViewSource.SortDescriptions>
                <scm:SortDescription PropertyName="Date" Direction="Ascending" />
            </CollectionViewSource.SortDescriptions>
        </CollectionViewSource>
    </Window.Resources>
    <Grid>
        <DataGrid IsReadOnly="True"
          ItemsSource="{Binding Source={StaticResource cvs}}"
          AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Date" Binding="{Binding Date, StringFormat='{}{0:dd.MM.yyyy}'}" />
            </DataGrid.Columns>
        </DataGrid>        
    </Grid>
</Window>

【讨论】:

  • 首先,感谢您的回答。我试过了,但遗憾的是它不起作用。你知道为什么它不起作用吗?此外,我没有绑定到 DataView,而是绑定到 DataTable。我不知道这是否有区别
  • 您能否提供一些可用于重现您的问题的示例数据?:stackoverflow.com/help/mcve。确保 Date 属性返回您期望的值。
  • 这是特定列的确切值(取自调试器):11.03.2017 00:00:00
  • 顺便说一句,感谢您指出发布问题的指南 :) 除了日期值之外,我不得不说我从 XML 序列化 DataTable,并且那里的值是 &lt;Date&gt;2017-03-11T00:00:00+01:00&lt;/Date&gt; 。然后我使用new DataTable.ReadXml() 读取XML,这显然将日期string 转换为已经特定于文化的11.03.2017
  • DataTable 中列的类型应该是 DateTime 以便您能够将列值排序为日期而不是字母字符串。
【解决方案2】:

您可以在列上使用SortMemberPath 来定义应该使用什么属性进行排序。

【讨论】:

  • 感谢您的建议,我还没有尝试过,因为我通过在获得数据时(在 MySQL 中)对数据进行排序解决了这个问题。但我感谢您的回答,并会尽快尝试
猜你喜欢
  • 2014-11-18
  • 1970-01-01
  • 2013-05-07
  • 1970-01-01
  • 2013-12-31
  • 2011-09-04
  • 2012-07-25
  • 2016-04-29
  • 1970-01-01
相关资源
最近更新 更多