如何格式化DataGridView中的DateTime列? [英] How to format DateTime columns in DataGridView?

查看:367
本文介绍了如何格式化DataGridView中的DateTime列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有对象数据绑定的DataGridView来显示系统中记录实体的信息,通过SOAP从远程服务中检索。
其中一列称为上一个操作,表示实体上次记录消息。它是一个 System.DateTime 值。当我阅读SOAP响应(如下面的例子)时,他的时间戳显然包含所有的信息,直到第二个分数。

I'm using a DataGridView with object data binding to display information about logging entities in a system, retrieved via SOAP from a remote service. One of the columns is called "Last action" and means the last time the entity logged a message. It is a System.DateTime value. When I read the SOAP response (example below), he timestamp obviously contains all the information, up to second fractions.

<LoggingEntity>
<host>marty86ce</host>
<process>10148</process>
<logger>http_core</logger>
<appName>httpd</appName>
<ffda>true</ffda>
<lastAction>2010-10-27T12:00:19.5117509Z</lastAction>
<lastHeartbeat>0001-01-01T00:00:00</lastHeartbeat>
<channelId>em_9BA2A2B4D0B6E66</channelId>
<ffdaChannelId>em_E7C8D1D4DE8EEB9</ffdaChannelId>
</LoggingEntity>

当我在桌子上显示时,我可以读取分钟
< img src =https://i.stack.imgur.com/dLYBz.pngalt =http://i.stack.imgur.com/dLYBz.png>
我使用以下代码当我按刷新按钮时,进行数据绑定

When I display it on the table, I instead can read up to the minutes I use the following code to do the data binding when I press the refresh button

public void RefreshEntities()
{
    IEntityManagement management = EntityPlugin.GetProxy();
    LoggingEntity[] result = management.FindLoggingEntities(new TemplateQuery { ffdaSpecified = true, ffda = true }); //Remote invocation

    Invoke(new MethodInvoker(delegate { gridEntities.DataSource = result; })); //THIS does the data binding from the array

    Invoke(new MethodInvoker(delegate { btnRefresh.Enabled = true; }));
}

我想知道如何控制数据绑定值的列格式。我认为格式dd / MM / yyyy hh:mm来自我的系统的设置。 如何以编程方式覆盖格式设置?

I would like to know how to control column formatting of data bound values. I think that the format dd/MM/yyyy hh:mm comes from my system's settings. How to override formatting settings programmatically?

提前谢谢

完整解决方案有关Logbus-ng的 Subversion (FFDAGui程序)的代码开源项目。

Full solution code on Subversion (FFDAGui program) for the Logbus-ng open source project.

推荐答案

如果是Windows格式的Datagrid,可以使用下面的代码格式化列的datetime

If it is a windows form Datagrid, you could use the below code to format the datetime for a column

dataGrid.Columns[2].DefaultCellStyle.Format = "MM/dd/yyyy HH:mm:ss";

编辑:

除此之外,如果您需要AM / PM格式的日期时间,可以使用以下代码

Apart from this, if you need the datetime in AM/PM format, you could use the below code

dataGrid.Columns[2].DefaultCellStyle.Format = "MM/dd/yyyy hh:mm:ss tt";

这篇关于如何格式化DataGridView中的DateTime列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆