如何格式化日期时间列中的DataGridView? [英] How to format DateTime columns in DataGridView?
问题描述
我使用的是对象数据绑定到显示在系统登录的实体,通过SOAP从远程服务检索到的信息一个DataGridView。
一列被称为最后动作以及用于最后一次实体记录的消息。这是一个的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>
当我在桌子上显示出来,而不是我可以读取到分钟
我用下面的code做数据绑定,当我preSS的刷新按钮
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来自于我的系统的设置。 如何重写编程格式设置?
感谢您提前
完整的解决方案code为Logbus-ng的开源项目。
Full solution code on Subversion (FFDAGui program) for the Logbus-ng open source project.
推荐答案
如果是Windows窗体DataGrid,可以使用下面的code格式化日期时间为列
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格式的日期时间,你可以使用下面的code
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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!