简体   繁体   English

如何在 DataGridView 中格式化日期时间列?

[英]How to format DateTime columns in DataGridView?

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.我正在使用带有对象数据绑定的 DataGridView 来显示有关系统中记录实体的信息,这些信息是通过 SOAP 从远程服务检索的。 One of the columns is called "Last action" and means the last time the entity logged a message.其中一列称为“上次操作”,表示实体上次记录消息的时间。 It is a System.DateTime value.它是一个System.DateTime值。 When I read the SOAP response (example below), he timestamp obviously contains all the information, up to second fractions.当我读取 SOAP 响应(下面的示例)时,他的时间戳显然包含所有信息,最多为第二个分数。

<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当我把它展示在桌子上时,我可以读取到分钟http://i.stack.imgur.com/dLYBz.pngI 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; }));
}

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.我认为格式 dd/MM/yyyy hh:mm 来自我的系统设置。 How to override formatting settings programmatically?如何以编程方式覆盖格式设置?

Thank you in advance提前谢谢你

Full solution code on Subversion (FFDAGui program) for the Logbus-ng open source project. Logbus-ng 开源项目的Subversion (FFDAGui 程序)上的完整解决方案代码。

If it is a windows form Datagrid, you could use the below code to format the datetime for a column如果它是 Windows 窗体 Datagrid,则可以使用以下代码来格式化列的日期时间

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

EDIT :编辑:

Apart from this, if you need the datetime in AM/PM format, you could use the below code除此之外,如果您需要 AM/PM 格式的日期时间,您可以使用以下代码

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

You can set the format you want:你可以设置你想要的格式:

dataGridViewCellStyle.Format = "dd/MM/yyyy";
this.date.DefaultCellStyle = dataGridViewCellStyle;
// date being a System.Windows.Forms.DataGridViewTextBoxColumn

Published by Microsoft in Standard Date and Time Format Strings :由 Microsoft 以标准日期和时间格式字符串发布

dataGrid.Columns[2].DefaultCellStyle.Format = "d"; // Short date

That should format the date according to the person's location settings.这应该根据人的位置设置格式化日期。

This is part of Microsoft's larger collection of Formatting Types in .NET .这是 Microsoft .NET 中更大的格式类型集合的一部分。

您可以在 aspx 中设置格式,只需在 BoundField 中添加属性“DateFormatString”。

DataFormatString="{0:dd/MM/yyyy hh:mm:ss}"

I used these code Hope it could help我使用了这些代码希望它可以帮助

dataGridView2.Rows[n].Cells[3].Value = item[2].ToString();
dataGridView2.Rows[n].Cells[3].Value = Convert.ToDateTime(item[2].ToString()).ToString("d");
string stringtodate = ((DateTime)row.Cells[4].Value).ToString("MM-dd-yyyy");
textBox9.Text = stringtodate;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM