4

My DataGridView date column is displaying a "dd/MM/yyyy 12:00:00 AM" format. I have looked and find this similar question, and used this method. But still the DataGridView is displaying all rows with 12:00:00 AM.

When I try to query the database

Select date from the database

Data Type of Date

But when I run the application and populate my DataGridView

The date displays with 12:00:00 AM

I tried adding this to my code, it still shows the 12:00:00 AM

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

or

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");
                }

If I change the for loop statement to

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

It throws an error.

CS1061 C# 'object' does not contain a definition for 'ToShortDateString' and no extension method 'ToShortDateString' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

But when I run it on a lower OS (W7)

It doesn't display 12:00:00 AM

Why in W10, where I developed the app and where the sql server is, it displays 12:00:00 AM?

Other similar problems I saw and tried the solutions but no luck.

Similar question

Similar question

Similar question

EDIT:

Windows 7 Region settings

Windows 7

Windows 10 Region settings

Windows 10

Jepher
  • 57
  • 1
  • 10
  • Because of different cultures/[regional settings] set on each machine. – SᴇM May 24 '18 at 10:51
  • _"C# 'object' does not contain a definition for 'ToShortDateString'"_ - `DataGridView` stores it's values inside objects, so first you need to cast `Value` to `DateTime`. – SᴇM May 24 '18 at 10:52
  • Time or no time should not be culture-dependent.. – TaW May 24 '18 at 10:52
  • Could you please, check culture of your machine. – Rajat Jaiswal May 24 '18 at 10:55
  • 2
    Setting the Column's Format should work, provided you do it to the right columns and after binding; it will not work if you do it before binding when the columns in the dgv are autogenerated, though.. – TaW May 24 '18 at 10:59
  • You've set the `DefaultCellStyle`, have you tried the Columns CellTemplate property (as in `dataGridViewColumn.CellTemplate.Style.Format = "dd-MM-yyyy"`)? – JayV May 24 '18 at 11:04
  • As for culture/region settings of my machines will update the question and post the a screenshot. In W7 it displays M/d/yyyy, while in W10 it displays d MMM yyyy – Jepher May 24 '18 at 11:05
  • I tried to match it on W7 it still attach 12:00:00 AM – Jepher May 24 '18 at 11:17
  • @JayV I already diid but the time still displaying. – Jepher May 24 '18 at 11:25
  • @TaW Yes I'm trying to change to format of Columns[11] or Columns["Date"] or Cells[11], they are all pointing the same column. And yes I'm formatting it after the binding. – Jepher May 24 '18 at 11:27
  • Everytime I have used a DataGridView I would setup the columns and their formats BEFORE I set its DataSource and also set AutoGenerateColumns = false – JayV May 24 '18 at 11:30
  • If I format my datagridview before setting its datasource, it will return me an error 'Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index' – Jepher May 24 '18 at 11:54
  • You need to add the columns before you can format them. – TaW May 24 '18 at 12:06
  • @Jepher hope you are binding the source only once. rebinding the datasource wont be retain the columns format which you specified. better you set the column formatting next line to the datasource binding. like `DataTable dt = new DataTable(); sda.Fill(dt); dataGridCust.DataSource = dt; dataGridCust.Columns["Date"].DefaultCellStyle.Format = "dd/MM/yyyy";` – Baskar John May 24 '18 at 12:23
  • @BaskarJohn. Yes, I tried 3 different format but still no luck. – Jepher May 24 '18 at 12:37
  • That sounds really strange. For testing: Put the code with a fany format into a button and see if it makes any difference! – TaW May 24 '18 at 13:02
  • @TaW. This also stressing me out, this is the only datagridview that is acting strange, it is inside a tabpage with splitcontainer (this is the only difference with my other tabpage that have datagridview). My other datagridview is displaying what I wanted to be. Will create a new one, in a different project, and see what will happen. – Jepher May 24 '18 at 13:08
  • Note that it should be visible to reflect any refreshes etc..!! (A tabpage bug, imo) – TaW May 24 '18 at 13:26
  • I simply created a table containing `Date` column, then using a `SqlDataAdapter` loaded data to `DataGridView`, after setting `DataSource` of `DataGridView` I changed format of the `Date` column of grid to `yyyy-MM-dd` using `dataGridView1.Columns["SomeDate"].DefaultCellStyle.Format = "yyyy-MM-dd";` and everything worked properly. I'm also using Windows 10. – Reza Aghaei May 24 '18 at 16:15
  • You should be able to reproduce the problem and share a reproduction code/steps here, otherwise the question is subject to close as off-topic, because of not having [MCVE] – Reza Aghaei May 24 '18 at 16:18
  • @RezaAghaei As I have said on the comments. This is only the datagridview that is having the problem. Its properties are the same with my other datagridview. I will try to create a new and similar project later. – Jepher May 25 '18 at 00:19
  • @Jepher In these cases creating MCVE at the first step will help you to find the problem and solution yourself. Then if you couldn't find the solution yourself, it helps other users to reproduce the problem and share try to solve the problem. Unless it's not clear the problem they are trying to solve is the same as the problem that you have. – Reza Aghaei May 25 '18 at 03:00
  • Deleted the previous dgv and created a new one, with same parameters. It is now displaying the right date. Why? Is it just a bug or something? I have spent hours thinking of a solution yesterday to fix the problem. Well, thank you very much for all your input. I'm still confuse why the dgv behaves like that. – Jepher May 25 '18 at 09:55
  • 1
    @Jepher It probably has been a misconfiguration in DataGridView. For example, if the `ValueType` of the `Column` is not set to `DateTime` then the format which you are trying to apply, will not work. Anyway, you should never forget creating a clean MCVE. It's the correct path to find and solve problems. – Reza Aghaei May 26 '18 at 10:30

0 Answers0