Thread.CurrentThread.CurrentCulture problem

T

Tsair

I set the thread culture in MAIN() as below in order to show the date in
format DAY/MONTH/YEAR, but the datagridview alway show the date in M/d/yyyy.

How to set the default Date format from windows xp control panel by C#
coding or is that any way to set the application date format to d/M/yyyy
without change the windows xp regional setting ?


Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US", false );

Thread.CurrentThread.CurrentCulture.DateTimeFormat.DateSeparator = "/";

Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern =
"d/M/yyyy";

Thread.CurrentThread.CurrentCulture.NumberFormat.NumberGroupSeparator = ",";

Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator =
".";

Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
 
D

Dave Sexton

Hi,

Try adding an event handler to the DataGridView.CellFormatting event. Here you can specify any format that you want.
 
T

Tsair

I have to set Each DataGridView.CellFormatting, is that any way to set the
Date Format to effect the whole application.

Thank you.
 
D

Dave Sexton

Hi,

I tested the following code and it worked for me:

CultureInfo culture = new CultureInfo("en-US");
culture.DateTimeFormat.ShortDatePattern = "d/M/yyyy";
culture.DateTimeFormat.ShortTimePattern = string.Empty;

System.Threading.Thread.CurrentThread.CurrentCulture = culture;
System.Threading.Thread.CurrentThread.CurrentUICulture = culture;

The reason why you need to set the ShortDatePattern and ShortTimePattern strings is because the DateTimeConverter, which is used by
the DataGridView cell that is formatting the DateTime into a string, uses the CurrentCulture to format the date in the general,
short-time format (g). The general format uses these two properties and concatenates the time to the date. Unfortunately, the
resulting string has a trailing space which seems to be unavoidable due to the string.Empty assignment, which removes the time from
the general format.

DateTime formatting on MSDN:
http://msdn2.microsoft.com/en-us/library/az4se3k1.aspx

For finer-grained control over the formatting of DateTimes in each cell of the DataGridView you must use the CellFormatting event as
I mentioned previously.
 
T

Tsair

Thanks Dave Sexton,

I use your methos and put in my program.cs, it does not work. My program.cs
code in like below, does it any mistake on my code.

static class Program

{

[STAThread]

static void Main()

{

CultureInfo culture = new CultureInfo("en-US");

culture.DateTimeFormat.ShortDatePattern = "d/M/yyyy";

culture.DateTimeFormat.ShortTimePattern = string.Empty;

System.Threading.Thread.CurrentThread.CurrentCulture = culture;

System.Threading.Thread.CurrentThread.CurrentUICulture = culture;

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

Application.Run(new Main());

}

}
 
T

Tsair

Sorry, Dave Sexton,

It work for me now, I got to set the DataGridView format to "d"

Thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top