如何为整个 winform 应用程序中使用的数据表设置日期时间格式?
How to set date time format for Datatables used in whole winform application?
我正在开发一个应用程序,我在其中为整个应用程序设置了默认区域性。
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
格式。
关于如何声明可在整个应用程序中使用的数据 table 的方法有很多种。您可以通过使用 DataTable 属性 创建静态 class 并在您的应用程序中的任何位置访问它来使用 Singleton。另一个是您可以使用依赖注入,通过将包含 DataTable 属性 的 class 实例注入您的 class 构造函数。 See Dry Principles explained
您可以将自定义 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;
}
}
}
但第一个例子更好。
我正在开发一个应用程序,我在其中为整个应用程序设置了默认区域性。
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
.
如何在一个地方应用 datetime
设置,以便所有 datatables
具有与默认区域性设置相同的 datetime
格式。
关于如何声明可在整个应用程序中使用的数据 table 的方法有很多种。您可以通过使用 DataTable 属性 创建静态 class 并在您的应用程序中的任何位置访问它来使用 Singleton。另一个是您可以使用依赖注入,通过将包含 DataTable 属性 的 class 实例注入您的 class 构造函数。 See Dry Principles explained
您可以将自定义 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;
}
}
}
但第一个例子更好。