Click here to Skip to main content
15,892,269 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I have a datagridview in vb.net windows application.

I have datetimepicker in one of the column. (Courtesy Microsoft)

How do I change the date format to dd/MM/yyyy ( Default is MM/dd/yyyy)

Normally to change the format we do as below
datagridview1.Columns(0).DefaultCellStyle.Format = "dd/MM/yyyy" which I have done


But as I have datetimepicker in the datagrid column it seems it is not applying the style.

An early help is highly appreciated.

Cheers
Anand
Posted
Updated 3-Sep-12 18:01pm
v3

This can help

Dim xRowCount As Integer = 0
Do Until xRowCount = xDataGrid.RowCount
xDataGrid.Rows(xRowCount).Cells(2).Style.Format = Format("dd-MMM-yyyy")
xRowCount = xRowCount + 1
Loop
 
Share this answer
 
Hi Anand,

For Datetimepicker date format. refer
http://stackoverflow.com/questions/10589877/format-datetimepicker-in-datagridview-column

http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/bb5ef524-b2ba-4420-82ac-230d156695aa[^]

For gridview date column dateformat
Their are two ways you can do that

1 You can write a Function on the code behind page like
Public Function ReturnOnlyDate(TDate As String) As String
Try
If TDate.Length = 19 Then ' 12/30/1899 00:00:13
Return (TDate.Remove(10, 9))
End If
Catch ex As Exception
Return Nothing
End Try
Return Nothing
End Function
and you should convert the date field in the gridview to Template field. and in the source page you change the bound field like this


<asp:templatefield headertext="Date" xmlns:asp="#unknown">

<asp:label id="Label2" runat="server" text="<%# ReturnOnlyDate(Eval("TDate"))%>">


<asp:label id="Label2" runat="server" text="<%# ReturnOnlyDate(Eval("TDate")))%>">




2. You can DIrectly convert the field to Template field and in the source page your can do like this.

<asp:templatefield headertext="Date" xmlns:asp="#unknown">

<asp:label id="Label2" runat="server" text="<%# Bind("TDate", "{0:MM/dd/yyyy}") %>">


<asp:label id="Label2" runat="server" text="<%# Bind("TDate", "{0:MM/dd/yyyy}") %>">




This could help.
 
Share this answer
 
Comments
Anand Rangaraju 6-Sep-12 6:19am    
Hi BheemG,

Have tried to do as suggested in the first link i.e.
http://stackoverflow.com/questions/10589877/format-datetimepicker-in-datagridview-column
but, it didn't worked as there are only limited date formats were available in the default cell style and they are not matching to my requirement (dd/MM/yyyy).

When I tried custom format also it didn't work out.

And on the other solutions of code behind pages.. My application is windows application not web application.

Anyway thanks for the reply.

Anand
BheemG 6-Sep-12 23:29pm    
Anand ,

In datetimepicker control your have a property called format select custom in it and you can enter the custom format on the custom format property of the datetimepicker to 'dd/MM/yyyy'.

Try this and let me know,

Happy coding.
DateTimePickers too can have date formats defined. Not sure which one you are using but it should have one of the property to specify the date format. Look for it and set as per need.

For MS datepicker: MSDN: DateTimePicker.CustomFormat Property [^]
Refer: MSDN: DateTimePicker Class[^]
 
Share this answer
 
Comments
Anand Rangaraju 3-Sep-12 9:33am    
Hi Sandeep,

First of all, thanks for the reply.

I could change the display format by setting in the following piece of code.
But the problem is the format is getting changed to MM/dd/yyyy again after the date selected.

Any idea how to keep the format in datagridview column after the date selection?

Public Sub New()
Me.Format = DateTimePickerFormat.Short
Me.CustomFormat = "dd/MM/yyyy"
Me.Format = DateTimePickerFormat.Custom
End Sub
This code is found in the below link.
http://msdn.microsoft.com/en-us/library/7tas5c80.aspx

Anand.
Sandeep Mewara 3-Sep-12 12:38pm    
For datetimepicker, use controls custom format and for datagridview use the column format value.
Anand Rangaraju 3-Sep-12 12:54pm    
Hi Sandeep,

I have already tried that as below

datagridview1.Columns(0).DefaultCellStyle.Format = "dd/MM/yyyy"

It didn't have any effect.

Please tell me if this is not the way you said.

Anand

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900