Datagridview日期格式显示 [英] Datagridview date format display

查看:268
本文介绍了Datagridview日期格式显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有从数据库返回的多条记录。其中之一是ScheduleDate列。返回的列是日期时间格式,我已使用代码将其转换为dd MMM yyyy格式。例如,转换后的输出将是201年7月11日。然后我将记录绑定到WinForm中的DataGridView。

但问题是格式会根据我的Windows操作系统时钟格式自行更改为07/11/2018。我没有在DataGridView的特定列中设置任何格式,就像我设置格式一样,每当我更改我的Windows操作系统时钟格式时,ScheduleDate数据都会陷入01/01/0001。我需要一次又一次地清理我的解决方案,这对最终用户造成了问题。

有没有其他方法可以用来解决我的问题?以下是我的代码示例:



Hi, I have multiple records returned from database. One of them is the ScheduleDate column. The column returned is in datetime format and I have converted it using codes into "dd MMM yyyy" format. For example, the converted output would be "11 Jul 2018". I then bind the records into DataGridView in WinForm.
But the problem is the format will change itself into "07/11/2018" as according to my Windows OS system clock format. I did not set any formatting in the particular column in DataGridView as if i set the formatting, whenever i change my Windows OS system clock format, the ScheduleDate data will haywire into "01/01/0001". I would need to clean my solution again and again and this has cause problem to end users.
Is there any alternative that i may use to solve my problem here? Below are some sample of my codes:

DateTime scheduleDate = new DateTime();
foreach (CustomerOrderDto customerOrderDto in customerOrderDtoList.DtoList)
{
    scheduleDate = DateTime.Parse(customerOrderDto.ScheduleDate);
    customerOrderDto.ScheduleDate = scheduleDate.ToString("dd MMM yyyy");
}





我的尝试:



1.删除datagridview列中的格式设置。

2.通过在最后一行代码中应用断点进行调试,并且ScheduleDate的格式仍然正确(2017年7月11日)。



What I have tried:

1. Remove formatting setting in datagridview column.
2. Debug by applying breakpoint at the last line of codes and ScheduleDate is still in correct format(11 Jul 2018).

推荐答案

您可以在Dto中创建一个新属性。
You could create a new property in your Dto.
public string ScheduleDateString { get { return ScheduleDate.ToString("dd MMM yyyy"); } }

然后将DataGridView列绑定到字符串类型属性。这不是一个可编辑的字段,因为它只有一个getter。会这样吗?

这就是说我想知道你为什么要强迫它这样做。系统正在执行它应该做的事情,在用户的设置中显示和允许编辑。因此,如果他们的设置是dd MMM yyyy,那就是它将显示的内容(并允许编辑)。

Then bind the DataGridView column to the string type property. This will not be an editable field since it only has a getter. Will that do?
That being said I'm wondering why you would want to force it to do this. The system is doing what it's supposed to do, displaying and allowing edit in the user's settings. So if their settings are dd MMM yyyy then that's what it will show (and editing will be allowed).


这篇关于Datagridview日期格式显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆