1

I have a DataGridView with a column that holds DateTime values like this:

wild DataGridView named dgView

The problem is in the selected row: it actually has the value "10/07/2015 12:00:00 am" but in the datagridview it is shown only "10/07/2015". The next row has the value of "10/07/2015 12:00:26 am". This problem happens only when the hour of the DateTime values is exactly midnight. When I pass that value to a textBox (to the left) with the following code, DateTime value is shown correctly (DateTime values are in the column with index 2):

text1.Text = dgView.SelectedRows[0].Cells[2].Value.ToString();

How can I make my DataGridView to show the complete DateTime values? I do not want to set the DefaultCellStyle.Format value because I want to show the DateTimes in the format of the OS regional settings (in my case is es-pe).

I'm targeting .Net 3.5 compiled for x86.

Community
  • 1
  • 1
Broken_Window
  • 2,037
  • 3
  • 21
  • 47

2 Answers2

3

You could specify the DefaultCellStyle.Format, but use a "standard format" which will localize appropriately:

DefaultCellStyle.Format = "g"; // General date, short time pattern

I don't know that that will format midnight "fully", but it's worth a try.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

How to force the DataGridView to show hours exactly at midnight (this answer comes from Jon Skeet's Answer):

In Visual Studio (I'm using VS Express 2013) select your DataGridView, go to Properties->DefaultCellStyle, enter "G" in the Format property and click Ok or Aceptar (mine is in spanish):

enter image description here

This solved my problem, and it respects OS regional settings.

Can I fangirl a little? OMYGOSHOMYGOSHOMYGOSHOMYGOSH Jon Skeet himself answered my question, yay! :D :D :D :D

Broken_Window
  • 2,037
  • 3
  • 21
  • 47