Click here to Skip to main content
15,119,285 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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");

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).
Updated 11-Jul-18 7:21am

1 solution

You could create a new property in your Dto.
public string ScheduleDateString { get { return ScheduleDate.ToString("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).
Jamie888 11-Jul-18 20:35pm
Sir, thanks for your suggestion. To answer your question above, originally the column ScheduleDate in datagridview has formatting done in "dd MMM yyyy" format. But users found out that if they change their system clock region(e.g. from English(US) into English(UK)), the supposed "dd MMM yyyy" value will get haywire into "01/01/0001". I have troubleshoot the issue by removing the formatting and move the formatting into codes level as the above question posted but to no avail as i keep on getting the "07/11/2018" instead of "11 Jul 2018" format. I suspect there is some setting in DataGridView that will default any formatting into the system clock format if there is no explicit formatting done in DataGridView itself.
Mike V Baker 12-Jul-18 10:36am
Did you go back and remove the custom formatting from the data grid view column after you moved the formatting to the code behind side? If that's just a text type property and you don't have the custom format still in place to make the DGV think it's supposed to be a date then it shouldn't do any formatting at all.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900