This is a migrated thread and some comments may be shown as answers.

formatting a datetime cell as date

9 Answers 1100 Views
GridView
This is a migrated thread and some comments may be shown as answers.
andi
Top achievements
Rank 1
andi asked on 18 Mar 2008, 05:39 PM
hello,

how can i format a cell that is currently displayed as "dd.mm.yyyy hh:mm:ss" to "dd..mm.yy" by code?

another cell that is displayed as "hh:mm:ss" should be displayed as "hh:mm".

thank you for the answer!

andreas

9 Answers, 1 is accepted

Sort by
0
Jack
Telerik team
answered on 19 Mar 2008, 10:24 AM
Hello andi,

Thank you for this question.

You can customize the cell's text formatting by using the DataTextFormatString property of the GridViewDataColumn.

Refer to the sample code below:

GridViewDateTimeColumn column = (GridViewDateTimeColumn)this.radGridView1.Columns["Date"]; 
column.DataTextFormatString = "{0:dd.mm.yy}"
column.DataTextFormatString = "{0:hh.mm}"
 

You should explicitly set the column type to be DateTime. Another possibility is manually adding a GridViewDateTimeColumn to the Columns collection.

I hope this helps. Do not hesitate to contact me, if you need further assistance.

Kind regards,
Jack
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
andi
Top achievements
Rank 1
answered on 28 Mar 2008, 02:31 PM
hello and thank you for the answer,

could you please post the codepart in your reply as visual basic code?

thank, andi
0
Jack
Telerik team
answered on 28 Mar 2008, 05:40 PM
Hi andi,

Here is the code in Visual Basic:

Dim column As GridViewDateTimeColumn = DirectCast(Me.radGridView1.Columns("Date"), GridViewDateTimeColumn) 
column.DataTextFormatString = "{0:dd.mm.yy}" 
column.DataTextFormatString = "{0:hh.mm}" 

Please, let me know if I can assist you further.

Sincerely yours,
Jack
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
andi
Top achievements
Rank 1
answered on 28 Mar 2008, 05:57 PM
thank you! now it works...

but there`s something strange with it: "{0:dd.mm.yy}" displays "00" for the month column, "{0:dd.MM.yy}" displays the correct month...

is this a bug?

andi
0
Pedro Valero Matas
Top achievements
Rank 1
answered on 29 Mar 2008, 07:04 AM
'mm' is the string format for minutes (hh:mm:ss) and 'MM' for month
0
Jack
Telerik team
answered on 31 Mar 2008, 08:28 AM
Hi andi,

Pedro is correct. Thank you for this suggestion.

You can find more about the date and time formatting on the MSDN site. Please follow the link below:

http://msdn2.microsoft.com/en-us/library/8kb3ddd4.aspx

Please contact me, if you have other questions.

Sincerely yours,
Jack
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
buda56
Top achievements
Rank 1
answered on 05 Jun 2008, 03:56 AM
Hi,
    The properties described are now obsolete when using 2008 Q1 you need to use the following:

private void gvConnections_CellFormatting(object sender, Telerik.WinControls.UI.CellFormattingEventArgs e)

{

GridViewDateTimeColumn column = (GridViewDateTimeColumn)this.gvConnections.Columns["IDate"];

column.FormatString =

"{0:dd/MM/yyyy}";

}

Regards..
Peter

0
macmariman
Top achievements
Rank 2
answered on 31 Aug 2009, 08:56 PM
I've tried the solution proposed by Buda56 but it is fires an stack overflow on the event. I'm using 2008 Q1 and I couldn't set properly a GridViewDateTimeColumn with this format "{0:dd/MM/yyyy}". Any suggestion?
Thanks in advance, regards

Martin
0
Jack
Telerik team
answered on 01 Sep 2009, 01:10 PM
Hello macmariman,

The described issue is addressed in our latest release - Q2 2009 SP1. I recommend that you try this version. It contains many improvements and exiting new features. We will be glad to help you with the transition process.

Sincerely yours,
Jack
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
GridView
Asked by
andi
Top achievements
Rank 1
Answers by
Jack
Telerik team
andi
Top achievements
Rank 1
Pedro Valero Matas
Top achievements
Rank 1
buda56
Top achievements
Rank 1
macmariman
Top achievements
Rank 2
Share this question
or