如何仅在使用SQL Server的数据库填充的datagridview中显示日期?

时间:2018-05-24 10:50:04

标签: c# sql-server winforms datagridview datetime-format

我的DataGridView日期列显示“dd / MM / yyyy 12:00:00 AM”格式。我查看并查找了this similar question,并使用了this method。但是DataGridView仍在显示12:00:00 AM的所有行。

当我尝试查询数据库时

Select date from the database

Data Type of Date

但是当我运行应用程序并填充我的DataGridView

The date displays with 12:00:00 AM

我尝试将其添加到我的代码中,它仍显示上午12:00:00

dataGridCust.Columns["Date"].DefaultCellStyle.Format = "dd/MM/yyyy"; //or
dataGridCust.Columns["Date"].DefaultCellStyle.Format = "dd-MM-yyyy"; //or
dataGridCust.Columns["Date"].DefaultCellStyle.Format = "d"; //or all of them

DataTable dt = new DataTable();
                sda.Fill(dt);
                dataGridCust.DataSource = dt;
                for (int i = 0; i < dataGridCust.Rows.Count; i++)
                {
                    dataGridCust.Rows[i].Cells[11].Value.ToString("d");
                }

如果我将for循环语句更改为

dataGridCust.Rows[i].Cells[11].Value.ToShortDateString();

它会抛出错误。

  

CS1061 C#'object'不包含'ToShortDateString'的定义,也没有扩展方法'ToShortDateString'接受类型'object'的第一个参数(你是否缺少using指令或汇编引用?)< / p>

但是当我在较低的操作系统(W7)上运行时

It doesn't display 12:00:00 AM

为什么在W10,我开发应用程序和sql server所在的位置,它显示在上午12:00:00?

我看到的其他类似问题并尝试过解决方案,但没有运气。

Similar question

Similar question

Similar question

编辑:

Windows 7区域设置

Windows 7

Windows 10区域设置

Windows 10

0 个答案:

没有答案
相关问题