【问题标题】:Excel sheet Column does not contain time in Date column But still showing times in DataGridView using c#Excel工作表列不包含日期列中的时间但仍使用c#在DataGridView中显示时间
【发布时间】:2012-06-06 13:02:34
【问题描述】:

我正在将 excel 工作表导入 DataGridView。我的 excel 工作表包含其中一列作为日期,此值为 12-05-2014。导入 DataGridView 后,它显示 12-05-2014 00:00:00。

让我如何在 Windows 应用程序的 DataGridView 中只显示日期而不显示时间

【问题讨论】:

    标签: c# excel date format formatting


    【解决方案1】:

    DateTime 始终包含日期和时间部分。您可以通过以下方式删除时间部分。

    在该列的 datagridview 中将 DefaultCellStyle.Format 字符串设置为 "d"

    或在 aspx 中使用 ToShortDateString

    <asp:TemplateColumn HeaderText="Upload Date">
        <ItemTemplate>
            <%# ((DateTime)DataBinder.Eval(Container.DataItem, "FileDate")).ToShortDateString() %>
        </ItemTemplate>
    </asp:TemplateColumn>
    

    dataGridView1.Columns[0].DefaultCellStyle.Format = "MM'/'dd'/'yyyy";
    

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

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

    只有在数据绑定之后才应该提及您的单元格格式。

    【讨论】:

    • 在将 DefaultCellStyle.Format 字符串设置为“d”后仍然显示 12-05-2014 00:00:00。
    • 只有在数据绑定之后才应该提及您的单元格格式。
    • 很抱歉我的回复...我在数据绑定后做了。但它也显示时间戳。让我知道有什么办法...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-24
    • 2016-06-02
    相关资源
    最近更新 更多