lemune

Hi all.

My problem is I'm having a datagridview that bind to table that has date format field.

I have manage to show the correct date, but when I edit or add new row, each time i leave the date column, it give error.

The error is because my current culture set short date with "MM-dd-yyyy", so i have to input the date like it.

But for showing i use

e.Value = CDate(e.Value).ToString("dd-MM-yyyy")

so datagridview show the data like it.

I want to make the input is also accept the "dd-MM-yyyy" format, and don't have to change the control panel regional setting.

How could I do that.

I try to use this code on Initialize the form:

Code Snippet

Dim dfi As DateTimeFormatInfo = New CultureInfo("en-US", False).DateTimeFormat
dfi.ShortDatePattern = "dd-MM-yyyy"
Thread.CurrentThread.CurrentCulture.DateTimeFormat = dfi


It give error:
Instance is read-only.
on
Thread.CurrentThread.CurrentCulture.DateTimeFormat = dfi

Any Suggestion

Thank you





Re: Windows Forms Data Controls and Databinding Error Datagridview binding with Date

BabyFace Jay

Put the following code at Program.cs just before the Application.Run();

Code Snippet
System.Threading.
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("zh-SG");

The data in the datagridview should be in dd/mm/yyyy.





Re: Windows Forms Data Controls and Databinding Error Datagridview binding with Date

lemune

Thank you it's work fine now.

And if we want the cultureinfo only on the spesific form, it also can done on new form before initialize.

For the list of CultureInfo check this: http://msdn2.microsoft.com/en-us/library/system.globalization.cultureinfo.aspx