From 0e605f68d3fd9e1a79c129f90aa8e0fb51d4b700 Mon Sep 17 00:00:00 2001 From: Jing Date: Sat, 29 Jul 2017 22:05:39 +0900 Subject: [PATCH] try to enhance datetime sort --- ReoGrid/Core/DataFormat.cs | 65 +++++++++++++++++++++----------------- ReoGrid/Core/FilterSort.cs | 4 +++ 2 files changed, 40 insertions(+), 29 deletions(-) diff --git a/ReoGrid/Core/DataFormat.cs b/ReoGrid/Core/DataFormat.cs index 88a43582..1d4d7218 100644 --- a/ReoGrid/Core/DataFormat.cs +++ b/ReoGrid/Core/DataFormat.cs @@ -916,9 +916,40 @@ public string FormatCell(Cell cell) bool isFormat = false; double number; + string pattern = Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern; DateTime value = baseStartDate; string formattedText = null; + CultureInfo culture = null; + + if (cell.DataFormatArgs is DateTimeFormatArgs) + { + DateTimeFormatArgs dargs = (DateTimeFormatArgs)cell.DataFormatArgs; + + culture = (dargs.CultureName == null + || string.Equals(dargs.CultureName, Thread.CurrentThread.CurrentCulture.Name)) + ? Thread.CurrentThread.CurrentCulture : new CultureInfo(dargs.CultureName); + + if (!string.IsNullOrEmpty(dargs.Format)) { + pattern = dargs.Format; + } + else + { + pattern = culture.DateTimeFormat.ShortDatePattern; + } + } + else + { + culture = System.Threading.Thread.CurrentThread.CurrentCulture; + cell.DataFormatArgs = new DateTimeFormatArgs { Format = culture.DateTimeFormat.ShortDatePattern, CultureName = culture.Name }; + } + + if (culture.Name.StartsWith("ja") && pattern.Contains("g")) + { + culture = new CultureInfo("ja-JP", true); + culture.DateTimeFormat.Calendar = new JapaneseCalendar(); + } + if (data is DateTime) { value = (DateTime)data; @@ -936,6 +967,11 @@ public string FormatCell(Cell cell) } catch { } } + else if (DateTime.TryParseExact(Convert.ToString(data), pattern, culture, DateTimeStyles.None, out value)) + { + isFormat = true; + //cell.InnerData = value; + } else { string strdata = (data is string ? (string)data : Convert.ToString(data)); @@ -963,35 +999,6 @@ public string FormatCell(Cell cell) cell.RenderHorAlign = ReoGridRenderHorAlign.Right; } - CultureInfo culture = null; - - string pattern = System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern; - - if (cell.DataFormatArgs != null && cell.DataFormatArgs is DateTimeFormatArgs) - { - DateTimeFormatArgs dargs = (DateTimeFormatArgs)cell.DataFormatArgs; - - if (pattern == null || pattern == String.Empty) - { - pattern = dargs.Format; - } - - culture = (dargs.CultureName == null - || string.Equals(dargs.CultureName, Thread.CurrentThread.CurrentCulture.Name)) - ? Thread.CurrentThread.CurrentCulture : new CultureInfo(dargs.CultureName); - } - else - { - culture = System.Threading.Thread.CurrentThread.CurrentCulture; - cell.DataFormatArgs = new DateTimeFormatArgs { Format = pattern, CultureName = culture.Name }; - } - - if (culture.Name.StartsWith("ja") && pattern.Contains("g")) - { - culture = new CultureInfo("ja-JP", true); - culture.DateTimeFormat.Calendar = new JapaneseCalendar(); - } - try { switch (pattern) diff --git a/ReoGrid/Core/FilterSort.cs b/ReoGrid/Core/FilterSort.cs index 02a0858b..92751c4c 100644 --- a/ReoGrid/Core/FilterSort.cs +++ b/ReoGrid/Core/FilterSort.cs @@ -453,6 +453,10 @@ private int CompareCell(int row, int col, object @base) { return data.CompareTo(Convert.ToString(@base)); } + else if (data is DateTime) + { + return (int)((DateTime)data).Ticks; + } else { try