【问题标题】:Need mm/dd/yyyy format in the first column of datatable数据表第一列需要mm/dd/yyyy格式
【发布时间】:2019-09-13 14:16:44
【问题描述】:

我正在从 excel 中读取值并将其存储在 Datagridview 中。数据表的第一列是日期,我需要 mm/dd/yyyy 格式,但它显示 mm/dd/yyyy 12:00:00 AM。我只需要日期部分。

我尝试过使用 defaultcellstyle.Format 和许多其他方法,但我没有找到解决方案

        OleDbConnection connection = new OleDbConnection(connectionstring);
        OleDbCommand command = new OleDbCommand("Select * From [POSFailures$]  WHERE Date =@param1", connection);
        command.Parameters.Add(new OleDbParameter("param1", todaydate));
        command.Connection = connection;
        try
        {
            connection.Open();
            OleDbDataAdapter sda = new OleDbDataAdapter(command);
            sda.Fill(dtexcel2);
            connection.Close();
        }

        catch (Exception)
        {

        }

        return dtexcel2;
    }

我已经尝试使用 for 循环和 google 中提到的所有内容,但我仍然没有得到想要的结果。任何解决方案都会有很大帮助!

【问题讨论】:

  • 您可以在使用 DefaultCellStyle.Format 后发布 DataGridView 的屏幕截图吗?我也没有在任何地方的代码中看到这一点。它还包含时间吗?
  • 在 format 属性之后,我仍然没有得到想要的格式
  • 我在任何地方都看不到您的格式代码。您可以在 DataGridView 上设置格式的位置发布代码吗?
  • 而且我没有在数据网格中添加任何列,因为我使用的是 excel 中的标题
  • 发布你的代码,我没有看到你在任何地方设置 DefaultCellStyle.Format = "MM/dd/yyyy" !如果您不告诉它您想要的格式,您如何期望它格式化?请发布代码。

标签: c# excel datagridview datatable


【解决方案1】:
yourDataGridView.Columns[0].DefaultCellStyle.Format = "MM/dd/yyyy";

【讨论】:

  • 试过了。但是没用
  • 我没有在数据网格中添加任何列,因为我正在使用 excel 中的标题来获取列名
  • 我收到语法错误[;预期] 在“{colName}_OLD_COLUMN_NAME”;以及 gridSource.Rows.Remove(oldColumn) 处的无效参数
  • 我在字符串前面加上了 $,它仍然显示 ;预计
  • 但是如果 $ 被删除,它适用于该行。 @DataColumn newColumn = gridSource.Columns.Add(colName);我收到此错误 - 名为“日期”的列已属于此数据表。
猜你喜欢
  • 2021-08-05
  • 1970-01-01
  • 2016-01-02
  • 1970-01-01
  • 1970-01-01
  • 2019-06-24
  • 1970-01-01
  • 2013-08-31
  • 2021-12-18
相关资源
最近更新 更多