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

DateTime Column displayed as LocalTime?

5 Answers 615 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Paul
Top achievements
Rank 1
Paul asked on 01 Dec 2009, 07:51 PM
Hello,

We have an application with SQL Server running in one location and then clients in different timezones using that data.  All datetimes are stored as UTC values.  For display we want them in local date/time.  How do we do that in GridView?

For example:
A grid showing a list of events with an updated_date column.  We want those event dates/times as local time.  Can .formatstring be used to somehow set ToLocalTime()?

Thanks,
Paul

5 Answers, 1 is accepted

Sort by
0
Dobry Zranchev
Top achievements
Rank 1
answered on 03 Dec 2009, 07:32 AM
Hi Paul,

You can use CellFormating event of the grid and there you can format the datetime as you want.

Kind regards,
Dobry Zranchev
0
Martin Vasilev
Telerik team
answered on 04 Dec 2009, 03:17 PM
Hello Paul,

Thank you for the question.

RadGridView does not support implicit UTC to Local DateTime conversion. You have two ways to work-around this:
- use CellFormatting event to change the cell displaying value as Dobry Zranchev mentioned.
- if you are using DataTable to specify DateTimeMode for the DateTime column.

However, we will consider adding such a property in GridViewDateTimeColumn as well for some of the feature releases. I have updated your Telerik points for bringing our attention to this. Do not hesitate to contact me again if you have any other questions.

Regards,
Martin Vasilev
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.
0
Dan
Top achievements
Rank 1
answered on 20 Mar 2014, 08:55 PM
I am doing this with the cellformatting method and the issue I have is the cellformatting is called everytime a click in the grid happens so it constantly subtracts the time offset from the current value, in essence it counts down the datetime on every click.

if((e.CellElement.ColumnInfo.HeaderText == "FallOff Date" ||
e.CellElement.ColumnInfo.HeaderText == "Created On" ||
e.CellElement.ColumnInfo.HeaderText == "Completed On"
) && e.CellElement.Value != null)
{
DateTime utcDate = DateTime.SpecifyKind((DateTime)e.CellElement.Value, DateTimeKind.Utc);
e.CellElement.Value = utcDate.ToLocalTime().ToString("MM/dd/yyyy hh:mm:ss tt");
}
0
Dan
Top achievements
Rank 1
answered on 20 Mar 2014, 10:26 PM
I have solved this. I was updating the Value which changes the underlying data object. For display only I should use the Text field of the cell.

e.CellElement.Text = utcDate.ToLocalTime().ToString("MM/dd/yyyy hh:mm:ss tt");
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 25 Mar 2014, 03:19 PM
Hello Dan,

Thank you for contacting Telerik Support.

Changing the cell value in the CellFormatting event is not a recommended approach. However, you can customize the CellElement.Text property as you have already found out. Please refer to our Formatting Cells help article for additional information. I think that our GridViewDateTimeColumn help article will be quite useful about formatting the date via the GridViewDateTimeColumn.FormatString.

I hope this information helps. Should you have further questions, I would be glad to help.
 
Regards,
Desislava
Telerik
 

Build cross-platform mobile apps using Visual Studio and .NET. Register for the online webinar on 03/27/2014, 11:00AM US ET.. Seats are limited.

 
Tags
GridView
Asked by
Paul
Top achievements
Rank 1
Answers by
Dobry Zranchev
Top achievements
Rank 1
Martin Vasilev
Telerik team
Dan
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or