Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 3-Sep-12 2:29am
Edited 3-Sep-12 19:01pm
_Amy51.5K
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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[^]
  Permalink  
Comments
Anand Rangaraju at 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 at 3-Sep-12 12:38pm
   
For datetimepicker, use controls custom format and for datagridview use the column format value.
Anand Rangaraju at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
Comments
Anand Rangaraju at 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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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
  Permalink  

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



Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 5 Jun 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100