By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,896 Members | 1,983 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,896 IT Pros & Developers. It's quick & easy.

Thread.CurrentThread.CurrentCulture problem

P: n/a
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.N umberGroupSeparator = ",";

Thread.CurrentThread.CurrentCulture.NumberFormat.N umberDecimalSeparator =
".";

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


Sep 26 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi,

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

--
Dave Sexton

"Tsair" <ls****@streamyx.comwrote in message news:%2****************@TK2MSFTNGP02.phx.gbl...
>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.N umberGroupSeparator = ",";

Thread.CurrentThread.CurrentCulture.NumberFormat.N umberDecimalSeparator = ".";

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


Sep 26 '06 #2

P: n/a
I have to set Each DataGridView.CellFormatting, is that any way to set the
Date Format to effect the whole application.

Thank you.

"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:eq**************@TK2MSFTNGP05.phx.gbl...
Hi,

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

--
Dave Sexton

"Tsair" <ls****@streamyx.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
>>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.DateTimeForma t.DateSeparator = "/";

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

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

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

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



Sep 27 '06 #3

P: n/a
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.CurrentCultu re = culture;
System.Threading.Thread.CurrentThread.CurrentUICul ture = 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.

--
Dave Sexton

"Tsair" <ls****@streamyx.comwrote in message news:ef**************@TK2MSFTNGP06.phx.gbl...
>I have to set Each DataGridView.CellFormatting, is that any way to set the
Date Format to effect the whole application.

Thank you.

"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message news:eq**************@TK2MSFTNGP05.phx.gbl...
>Hi,

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

--
Dave Sexton

"Tsair" <ls****@streamyx.comwrote in message news:%2****************@TK2MSFTNGP02.phx.gbl...
>>>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.DateTimeForm at.DateSeparator = "/";

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

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

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

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




Sep 27 '06 #4

P: n/a
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.CurrentCultu re = culture;

System.Threading.Thread.CurrentThread.CurrentUICul ture = culture;

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(fals e);

Application.Run(new Main());

}

}

Sep 29 '06 #5

P: n/a
Sorry, Dave Sexton,

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

Thank you.
Sep 29 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.