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

Cast int to DateTime

5 Answers 130 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Riccardo
Top achievements
Rank 1
Riccardo asked on 21 Jul 2010, 09:17 AM
Hi all,

i have a grid with an object DataSource.
This DataSource read a table with date write in integer format like that: 20100721.
I want to know if is possible to display in DateTime format 21/07/2010 and edit with a RadDateTimePicher.

Thanks in advance.

Riccardo Fanetti

5 Answers, 1 is accepted

Sort by
0
Tsvetoslav
Telerik team
answered on 22 Jul 2010, 05:00 PM
Hi Riccardo,

You can convert the int value to a string and then use string operations to extract the year, month and day portion to construct a new DateTime object.

Hope it helps.

Greetings,
Tsvetoslav
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Riccardo
Top achievements
Rank 1
answered on 22 Jul 2010, 06:57 PM
Hi,

sorry but i'm new to telerik component and i don't know how to cast an int to a string.
I have this problem also if i want to show a checkbox in a grid and the data in the database are int.

Can you send me an example ?

Thanks in advance.
Riccardo Fanetti.
0
Tsvetoslav
Telerik team
answered on 23 Jul 2010, 12:27 PM
Hello Riccardo,

Here are a code snippet for the conversion:

int date = 20100710;
string strDate = date.ToString();
DateTime realDate = new DateTime(Convert.ToInt32(strDate.Substring(0, 4)), Convert.ToInt32(strDate.Substring(4, 2)), Convert.ToInt32(strDate.Substring(6, 2)));


All the best,
Tsvetoslav
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Riccardo
Top achievements
Rank 1
answered on 28 Jul 2010, 09:12 AM
Hi Tsvetoslav,

my problem it's not to convert a int value to date value...
My problem it's i want to convert this value directly in the grid column.

Situation:
- I have a object datasource with int datetime.
- I want to bind on a grid directly and edit with radDateTimepicker.

It's possible ?

How ? 

Thank's in advance.
Riccardo
0
Tsvetoslav
Telerik team
answered on 29 Jul 2010, 12:05 PM
Hi Riccardo,

RadGrid is a data-bound control and you should pass to it the data in the type in which you need it in order to have insert/update/delete, filtering, sorting, etc. operations on it. Therefore, in your particular case, upon getting the data from the database you should convert the given int column values to data-time and construct a new data object which should be passed to the grid's DataSource property. So, for example, if your ObjectDataSource is returning a collection of object which type has, let's say, a property of type int (but signifying actually a date-time value) then you need to construct another collection of different objects and replace the int property with a date-time one. Then use the data in the old collection to populate the new one with the values of the property in question being converted from int to DateTime.

I hope this information will prove helpful.

All the best,
Tsvetoslav
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Grid
Asked by
Riccardo
Top achievements
Rank 1
Answers by
Tsvetoslav
Telerik team
Riccardo
Top achievements
Rank 1
Share this question
or