【问题标题】:How to set date time format for Datatables used in whole winform application?如何为整个 winform 应用程序中使用的数据表设置日期时间格式?
【发布时间】:2019-03-12 05:50:10
【问题描述】:

我正在开发一个为整个应用程序设置默认文化的应用程序。

static void Main()
{
    try
    {
        #region set date pattern for whole application
        try
        {
            CultureInfo culture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
            culture.DateTimeFormat.ShortDatePattern = System.Configuration.ConfigurationManager.AppSettings["ShortDateFormat"];
            culture.DateTimeFormat.LongDatePattern = System.Configuration.ConfigurationManager.AppSettings["LongDateFormat"];

            CultureInfo.DefaultThreadCurrentCulture = culture;
            CultureInfo.DefaultThreadCurrentUICulture = culture;
        }
        catch (Exception ex)
        {
            RCOP.SmartClient.General.SaveLog(ex);
        }
        #endregion
    }
    ....
}

日期时间格式不适用于datatables,并且datatables 中的日期列具有计算机中设置的日期格式,如果用户更改计算机datetime 格式,则会导致异常。所以我必须为个人datatables使用以下代码

dataTable.Locale.DateTimeFormat.ShortDatePattern = Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern;
dataTable.Locale.DateTimeFormat.LongDatePattern = Thread.CurrentThread.CurrentCulture.DateTimeFormat.LongDatePattern;

问题是我必须在我使用过datatables的整个应用程序中应用上述设置(超过200个地方,因为win应用程序非常大)。

如何在一处应用datetime 设置,以便所有datatables 具有与默认区域性设置相同的datetime 格式。

【问题讨论】:

    标签: c# winforms datatables cultureinfo


    【解决方案1】:

    您可以将自定义DataTable 与自定义culture 始终设置为ShortDatePattern,因为您的DataTable 格式仅用于显示日期,而Locale 不是static 它不能在应用程序中被全局继承或覆盖,每个DataTable 都有自己的DateTimeFormat
    因此,请执行以下操作:

    [Serializable]
    public class MyDataTable : DataTable
    {
        void SetCulture()
        {
            this.Locale.DateTimeFormat.ShortDatePattern = Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern;
            this.Locale.DateTimeFormat.LongDatePattern = Thread.CurrentThread.CurrentCulture.DateTimeFormat.LongDatePattern;
        }
        public MyDataTable()
            : base()
        {
            SetCulture();
        }
    
        public MyDataTable(string tableName)
            : base(tableName)
        {
            SetCulture();
        }
    
        public MyDataTable(string tableName, string tableNamespace)
            : base(tableName, tableNamespace)
        {
            SetCulture();
        } 
    
        /// <summary>
        /// Needs using System.Runtime.Serialization;
        /// </summary>
        public MyDataTable(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
            SetCulture();
        } 
    }
    

    并在整个应用程序中将所有DataTable 替换为MyDataTable
    或者这样做:

    public interface ISource
    {
        DataTable Table { get; }
    } 
    public class MySource : ISource
    {
        private DataTable table;
        public DataTable Table
        {
            get
            {
                if (table == null)
                {
                    table = new System.Data.DataTable();
                    table.Locale.DateTimeFormat.ShortDatePattern = Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern;
                    table.Locale.DateTimeFormat.LongDatePattern = Thread.CurrentThread.CurrentCulture.DateTimeFormat.LongDatePattern;
                }
                return table;
            }
            private set
            {
                this.table = value;
            }
        } 
    }
    

    但第一个例子更好。

    【讨论】:

      【解决方案2】:

      您可以通过多种方式声明可在整个应用程序中使用的数据表。您可以通过创建具有 DataTable 属性的静态类并在应用程序的任何位置访问它来使用 Singleton。另一个是您可以使用依赖注入,通过将包含 DataTable 属性的类的实例注入到您的类构造函数中。 See Dry Principles explained

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-01-07
        • 1970-01-01
        • 1970-01-01
        • 2015-01-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多