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

时间:2018-12-07 15:38:13

标签: c# winforms datagrid devexpress

我想将DisplayFormat中的grid更改为

!localTime.isAfter(...)

"MM/dd/yyyy"

但什么也没发生。

3 个答案:

答案 0 :(得分:0)

您应该使用此示例(示例):

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时,会将其转换为具有选定格式的字符串。

以下是多种日期格式以及如何使用它们的链接:https://www.dotnetperls.com/datetime-format

答案 1 :(得分:0)

与我的复杂程序中孤立的工作代码相比,回答我的问题要容易得多。因此,这是我的程序中可以使用的技巧:

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事件中捕获了CustomDrawCell对象,并使用两种标准方法DateTime.ToShortDateString()DateTime.ToLongTimeString().将其转换为字符串,非常感谢。

答案 2 :(得分:0)

Settings

设置显示格式

dd-MMM-yyyy hh:mm:ss tt

Result