如何在 DataGridView 中格式化日期时间列? [英] How to format DateTime columns in DataGridView?
问题描述
我正在使用带有对象数据绑定的 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>
当我把它显示在桌子上时,我可以读取到分钟当我按下刷新按钮时,我使用以下代码进行数据绑定
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?
提前致谢
Subversion 上的完整解决方案代码(FFDAGui 程序)用于 Logbus-ng 开源项目.
Full solution code on Subversion (FFDAGui program) for the Logbus-ng open source project.
推荐答案
如果是windows窗体Datagrid,可以使用下面的代码来格式化列的日期时间
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 中格式化日期时间列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!