简体   繁体   English

将转换后的String排序为DateTime列。

[英]Sorting a converted String to DateTime column.

My DataTable has a Date column which must accept several different DateTime formats. 我的DataTable有一个Date列,该列必须接受几种不同的DateTime格式。 I'd like to consolidate these under one form and then sort accordingly by date. 我想将这些合并为一种形式,然后按日期进行相应排序。

So far I've written the code below to change the way the date is displayed. 到目前为止,我已经编写了以下代码来更改日期的显示方式。 But it still doesn't sort properly. 但是它仍然不能正确排序。 What's displayed isn't what's used when sorting. 显示的内容不是排序时使用的内容。 It still uses the old format. 它仍然使用旧格式。

So 2018-05-30 still goes after 2018-09-05 because the system is reading the data before it was converted as 30/05/2018 and 05/09/2018. 因此2018-05-30仍会在2018-09-05之后,因为系统正在读取数据,然后将其转换为30/05/2018和05/09/2018。 and because 30 > 05 it sorts improperly. 并且由于30> 05,因此排序不正确。 Anyone have any suggestions? 有人有什么建议吗?

        if (e.PropertyName.Contains("Date"))
        {
            DataGridTextColumn dgtc = e.Column as DataGridTextColumn;
            DateTimeConverter con = new DateTimeConverter();
            (dgtc.Binding as Binding).Converter = con;
        }


    public class DateTimeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                try
                {
                    return DateTime.Parse(value.ToString()).ToString("yyyy-MM-dd");
                }
                catch
                {
                    return value;
                }
            }
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && value.GetType() == typeof(System.DateTime))
            {
                DateTime t = (DateTime)value;
                return t.ToShortDateString();
            }
            return value;
        }


    }

have you tried 你有没有尝试过

List<DateTime> yourdateTime = new List<DateTime>();
yourdateTime.OrderBy(x => x.Date);

also look at 也看

yourdateTime.OrderBy(x => x.Date.TimeOfDay)
.ThenBy(x => x.Date.Date)
.ThenBy(x => x.Date.Year);

once you are into the Date Class you have lots of ways to sort it. 进入Date Class您可以通过多种方式对其进行排序。 just make sure if you use more than one to use OrderBy first and ThenBy for subsequent passes. 只需确保是否使用多个来先使用OrderBy ,然后再使用ThenBy进行后续ThenBy

Set the SortMemberPath property of the DataGridColumn to "Date" if this is what your date column/property is called. 如果您的日期列/属性被调用,则将DataGridColumnSortMemberPath属性设置为“ Date”。 Then it should sort the column by this property regardless of the formatting. 然后,无论格式如何,都应按此属性对列进行排序。

But you don't need to use a converter to format the date. 但是您不需要使用转换器来格式化日期。 You could just set the StringFormat property of the binding: 您可以只设置绑定的StringFormat属性:

if (e.PropertyName.Contains("Date"))
{
    DataGridTextColumn dgtc = e.Column as DataGridTextColumn;
    dgtc.Binding = new Binding(e.PropertyName) { StringFormat = "yyyy-MM-dd" };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM