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

GridDateTimeColumn value as DateTime...

5 Answers 645 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Benny
Top achievements
Rank 1
Benny asked on 16 Jul 2009, 10:49 AM
I like to get the values with the right type (not strings) from GridDateTimeColumn and GridNumericColumn.
 GridColumnCollection columns = e.Item.OwnerTableView.Columns;     
        foreach (GridColumn column in columns) 
            { 
                if (column is GridDateTimeColumn) 
                { 
 
                    if (startdatetime.Equals(column.UniqueName)) 
                    { 
                        // How do i get the value as a DateTime?
 
                    } 
                    else 
                    { 
                        throw new Exception( 
                            String.Format("No implementation for column.UniqueName: {0} (UpdateResourceTimeOff) "
                                          column.UniqueName)); 
                    } 
                } 
                else if (column is GridNumericColumn) 
                { 
 
                    if ("someTextString".Equals(column.UniqueName)) 
                    { 
                        // How do i get the double value 
                    } 
                    else 
                    { 
                        throw new Exception( 
                            String.Format("No implementation for column.UniqueName: {0} (UpdateResourceTimeOff) "
                                          column.UniqueName)); 
                    } 
                } 


5 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 16 Jul 2009, 12:46 PM
Hi Benny,

You can try the following code snippet to get the row value in DateTime format.
 
DateTime dateValue =  Convert.ToDateTime((e.Item as GridDataItem)["ColumnUniquName"].Text); 

-Shinu.
0
Benny
Top achievements
Rank 1
answered on 16 Jul 2009, 12:55 PM
By default I hate to cast strings to Date... It will fail in a lot of cases... When the time format is different depending on local settings on client server etc. The same thing goes for numbers. In some countries a dot (.) is treated as thousand separator and in other as coma separator.

As I wrote not as a string that you cast as in Shinu example.. :-(

But thanks for answering!

 /Benny
0
Daniel
Telerik team
answered on 16 Jul 2009, 09:00 PM
Hello Benny,

In ItemDataBound you can get the value this way:
DateTime dt = (DateTime)DataBinder.Eval(e.Item.DataItem, "myDateTimeValue"); 
DataBinder.Eval returns an object so you should cast it to DateTime.

Alternatively you can extract the values from the item:
if(e.Item is GridDataItem) 
   GridDataItem item = e.Item as GridDataItem; 
   Hashtable table = new Hashtable(); 
   item.ExtractValues(table); 
   DateTime dt = (DateTime)table["myDateTimeValue"]; 
 


Best regards,
Daniel
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Benny
Top achievements
Rank 1
answered on 17 Jul 2009, 02:57 PM
Hi Daniel,
I tried what you wrote but it doesn't work (at least not with german local settings). 

//  Can't unbound..... 
//   timeOff.startDateTime = (DateTime) values[Startdatetime]; 
 
// This works but then I'm back useing strings :-( 
timeOff.startDateTime = Convert.ToDateTime( values[Startdatetime]); 

0
Daniel
Telerik team
answered on 23 Jul 2009, 11:37 AM
Hello Benny,

I'm afraid that the provided information is not sufficient to provide an appropriate answer for you.

What do you mean by "it doesn't work"? Have you got an error message? On which event the posted code snippets get executed?

Regards,
Daniel
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
Grid
Asked by
Benny
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Benny
Top achievements
Rank 1
Daniel
Telerik team
Share this question
or