Click here to Skip to main content
13,150,867 members (44,056 online)
Rate this:
Please Sign up or sign in to vote.

I have a datagridview in 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.

Posted 3-Sep-12 1:29am
Updated 3-Sep-12 18:01pm
Rate this: bad
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[^]
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.

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.

Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Hi Anand,

For Datetimepicker date format. refer[^]

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
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.
Anand Rangaraju 6-Sep-12 6:19am
Hi BheemG,

Have tried to do as suggested in the first link i.e.
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.

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.
Rate this: bad
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

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.170924.2 | Last Updated 5 Jun 2014
Copyright © CodeProject, 1999-2017
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