【问题标题】:How to format DateTime columns in DataGridView?如何在 DataGridView 中格式化 DateTime 列?
【发布时间】:2010-10-27 12:26:05
【问题描述】:

我正在使用带有对象数据绑定的 DataGridView 来显示有关系统中记录实体的信息,这些信息是通过 SOAP 从远程服务中检索的。 其中一列称为“上次操作”,表示实体上次记录消息的时间。这是一个System.DateTime 值。当我阅读 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>

当我把它展示在桌子上时,我最多可以读到分钟 当我按下刷新按钮时,我使用以下代码进行数据绑定

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 格式来自我的系统设置。 如何以编程方式覆盖格式设置?

提前谢谢你

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

【问题讨论】:

  • 如何在 WPF 中做到这一点?

标签: c# winforms data-binding datetime datagridview


【解决方案1】:

如果是windows窗体Datagrid,你可以使用下面的代码来格式化一列的日期时间

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

编辑:

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

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

【讨论】:

    【解决方案2】:

    【讨论】:

      【解决方案3】:

      你可以设置你想要的格式:

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

      【讨论】:

      • dd/MM/yyyy 将日期格式化为dd-MM-yyyy。使用dd'/'MM'/'yyyy 将日期格式化为dd/MM/yyyy
      • yourColumn.DefaultCellStyle = new DataGridViewCellStyle { Format = "dd'/'MM'/'yyyy hh:mm:ss" };
      • 不幸的是,它对我没有任何改变。我的约会保持原样。看起来我的列中的日期被视为一个字符串,这使得它不可格式化。除了点之外的日期是否有特殊要求,它们是真实的日期值?
      • 即使对我来说它也不起作用,我尝试了这里提到的所有方法。我正在使用 BindingSource!
      • 对不起,你的dataGridViewCellStyle 是什么?
      【解决方案4】:

      微软发布于Standard Date and Time Format Strings:

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

      这应该根据人的位置设置格式化日期。

      这是 Microsoft 更大的 Formatting Types in .NET 集合的一部分。

      【讨论】:

        【解决方案5】:

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

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

        【讨论】:

        • System.Windows.Forms.DataGridViewCell中没有这样的 DateFormatString 属性
        【解决方案6】:

        我使用了这些代码希望它可以帮助

        dataGridView2.Rows[n].Cells[3].Value = item[2].ToString();
        dataGridView2.Rows[n].Cells[3].Value = Convert.ToDateTime(item[2].ToString()).ToString("d");
        

        【讨论】:

        • 你能补充一些解释吗?
        • 这是如果您手动填充行,并且您希望将 DateTime 转换为字符串。 ToString("d") 从 DateTime ToString 方法中指定仅日期格式。
        【解决方案7】:
        string stringtodate = ((DateTime)row.Cells[4].Value).ToString("MM-dd-yyyy");
        textBox9.Text = stringtodate;
        

        【讨论】:

        • 您能否解释一下为什么以及如何解决这个问题?
        • 数据绑定列时应该设置格式字符串。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-28
        • 1970-01-01
        • 1970-01-01
        • 2014-03-08
        • 2012-04-17
        相关资源
        最近更新 更多