无法修改数据集

时间:2013-01-09 13:17:47

标签: c# asp.net mysql datatable dataset

这是我的一部分c#代码

if (dsResult != null && dsResult.Tables[0].Rows.Count > 0)
{
    foreach (DataRow dr in dsResult.Tables[0].Rows)
    {
        dr["Date"] = Convert.ToDateTime(dr["Date"]).ToString("yyyy-MM-dd");
    }
}
dsResult.Tables[0].AcceptChanges();

我要做的是数据集的格式为{/ 1}} d / m / yyyy我正在尝试修改数据格式为Date,但我的数据集没有得到更新。

1 个答案:

答案 0 :(得分:2)

日期 存储的方式以及显示的方式是两个截然不同的事情。例如,它在1/7/2013 00:00:00 AM中显示为DataTable的原因是因为它根据您的文化设置显示DateTime的默认显示。

然而,这通常不是你想要它的显示方式,所以有很多方法可以给这只猫留下光彩,但我会给你一些。首先,您可以简单地更改应用程序的文化,以便在默认情况下以您希望的方式显示它。

CultureInfo culture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
culture.DateTimeFormat.ShortDatePattern = "yyyy-MM-dd";
Thread.CurrentThread.CurrentCulture = culture;

现在,当您显示DateTime值时,默认情况下您会在发布时获得该文化:

var dt = new DateTime();
dt.ToShortDateString(); // here is where the culture is used

您可能还会发现在应用程序中有一个特定的位置需要以某种方式显示它,您可以这样做:

var dt = new DateTime();
dt.ToString("dd-MM-yyyy"); // custom formatting on the fly

简而言之,您不需要DataTable中更改它。无论如何显示 都是相同的。

相关问题