如何在 GridView 中以不同方式显示日期?

How to show date in diffrent way in GridView?

我想从

更改为 DisplayFormat in my grid
"MM/dd/yyyy"

"yyyy/MM/dd hh:mm:ss"

但是没有任何反应。

你应该使用这个(例子):

static void Main()
{
    DateTime time = DateTime.Now;             // Use current time.
    string format = "yyyy/MM/dd hh:mm:ss";    // Use this format.
    Console.WriteLine(time.ToString(format)); // Write to console.
}

因此,在 GridView 中插入数据时,您将其转换为具有所选格式的字符串。

这里是 link 多日期格式及其使用方法:https://www.dotnetperls.com/datetime-format

对我来说,回答我的问题比在我的复杂程序中孤立工作代码更容易。所以这是我在我的程序中运行的技巧:

private void GridView_CustomDrawEvent(object sender, RowCellCustomDrawEventArgs e)
{
    if(e.Column.FieldName == "CreationDate")
    {
        string date = ((DateTime) view.GetRowCellValue(e.RowHandle, "CreationDate"));
        string displayDate = date.ToShortDateString() + " " time.ToLongTimeString();
        e.DisplayText = displayDate;
    }
}

我赶上了 DateTime object in CustomDrawCell event and I am translating it to string using two standard methods DateTime.ToShortDateString() and DateTime.ToLongTimeString(). 非常感谢您的宝贵时间。

Settings

设置显示格式

dd-MMM-yyyy hh:mm:ss tt

Result