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

DateTime column formatting

5 Answers 577 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Phi
Top achievements
Rank 1
Phi asked on 02 Jul 2007, 03:31 AM

Hi,

I have a DateTime column on a RadGridView. How do I set formatting for that column?
I have tried adding GridViewDataColumn and GridViewDateTimeColumn to the MasterGridViewTemplate, then seting the DataTextFormatString property of the column to "{0:dd/MMM/yyyy}";
but that doesn't work.

Currently I am doing a work-around, where I subscribe to the OnGridViewCellFormatting event, detect if it's a date value and re-format it there, but this solution isn't really ideal.

Please advise!

Regards,
Phi Le

5 Answers, 1 is accepted

Sort by
0
Peter
Telerik team
answered on 03 Jul 2007, 08:22 AM
Hi Phi,

Thank you for question.
 
You can set the data bound column formatting with the following code:

GridViewDataColumn newColumn = new GridViewDataColumn("Date");//databound column
this.radGridView1.MasterGridViewTemplate.Columns.Add( newColumn );
newColumn.DataTextFormatString = "{0:dd/MMM/yyyy}";

If the column is not a data bound column, you should subscribe for GridViewCellFormatting event and reformat the value dynamically like you did it.
 

Greetings,
Peter
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Phi
Top achievements
Rank 1
answered on 03 Jul 2007, 10:57 PM

Hello! Thanks for your response.

My columns are databound, so actually I did follow your suggestion originally. Here is the code I used:

DataTable dt = new DataTable();  
dt.Columns.Add("col1"typeof (int));  
dt.Columns.Add("col2"typeof (DateTime));  
 
dt.Rows.Add(new object[] {1, DateTime.Now});  
dt.Rows.Add(new object[] {2, DateTime.Now.AddDays(1.0)});  
 
this.radGridView1.MasterGridViewTemplate.AutoGenerateColumns = false;  
 
GridViewDataColumn col1 = new GridViewDataColumn("col1");  
this.radGridView1.MasterGridViewTemplate.Columns.Add(col1);  
 
GridViewDataColumn col2 = new GridViewDataColumn("col2");  
this.radGridView1.MasterGridViewTemplate.Columns.Add(col2);  
col2.DataTextFormatString = "{0:dd/MMM/yyyy}";  
 
this.radGridView1.DataSource = dt; 

but the date column still doesn't format correctly. Is there something I have missed?

Cheers
Phi
0
Peter
Telerik team
answered on 04 Jul 2007, 12:48 PM
Hello Phi,

We could not reproduce this issue - I have attached the files I have used for my tests. It may be best to send us your application, so that we can review it and provide a proper solution.

At this time, I have only one suggestion - if you want to use special editor for datetime, please replace

 new GridViewDataColumn( "col2" )

with

 new GridViewDateTimeColumn( "col2" )


Regards,
Peter
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Phi
Top achievements
Rank 1
answered on 04 Jul 2007, 11:05 PM
Hi Peter!

I see the problem now, I was using a standard GridViewDataColumn instead of the GridViewDateTimeColumn.

It appears the datetime formatting doesn't work if you do not use the GridViewDateTimeColumn. Maybe this could be something you could look into.

Thanks very much for clearing this up.

Cheers
Phi
0
Peter
Telerik team
answered on 05 Jul 2007, 07:35 AM
We are glad to be of help, Phi! If we could be of further assistance, please do not hesitate to write us back.

 
Greetings,
Peter
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
Tags
GridView
Asked by
Phi
Top achievements
Rank 1
Answers by
Peter
Telerik team
Phi
Top achievements
Rank 1
Share this question
or