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

datetime in GridCalculatedColumn

5 Answers 251 Views
Grid
This is a migrated thread and some comments may be shown as answers.
ketan reddy
Top achievements
Rank 1
ketan reddy asked on 22 Jun 2009, 08:04 PM
Hi,

I have two GridDateTimeColumns in a telerik grid--one the start date and other the End date. Now I want a 3rd column which would calculate the difference between the 2 columns.


 <telerik:GridCalculatedColumn Aggregate="Custom" DataType="System.TimeSpan" DataFields="DATE_EXPIRED,TRIAL_REQUEST_DATE" Expression="{0}-{1}"  UniqueName="column"></telerik:GridCalculatedColumn> 
                          
<telerik:GridDateTimeColumn DataField="DATE_EXPIRED" DataFormatString="{0:d}"  HeaderText="Expired" UniqueName="DATE_EXPIRED" ></telerik:GridDateTimeColumn> 
 
<telerik:GridBoundColumn DataField="TRIAL_REQUEST_DATE" HeaderText="Requested" SortExpression="TRIAL_REQUEST_DATE"  DataFormatString="{0:d}" UniqueName="TRIAL_REQUEST_DATE">   
 </telerik:GridBoundColumn> 


I get the following error:

Cannot perform '-' operation on System.DateTime and System.DateTime.

What parameters should I be using in the Expression attribute to achieve this? any ideas?


Thanks,
Ketan

5 Answers, 1 is accepted

Sort by
0
Sebastian
Telerik team
answered on 25 Jun 2009, 02:26 PM

Hello Ketan,

I am afraid that the ASP.NET AJAX framework and DataColumn.Expression in particular do not support this type of subtraction for DateTime values. That is why the Expression defined for the GridCalculatedColumn is not considered as valid at compile time.

Instead you may consider programmatic approach with the DateTime.Subtract method of the framework explained in this MSDN section:
http://msdn.microsoft.com/en-us/library/8ysw4sby.aspx

Let me know if I am missing something.

Kind regards,

Sebastian
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
William
Top achievements
Rank 1
answered on 08 Jun 2011, 08:00 PM

Hi Sebastian,

Do you mean:

<telerik:GridCalculatedColumn Aggregate="Custom" DataType="System.TimeSpan" DataFields="DATE_EXPIRED,TRIAL_REQUEST_DATE" Expression="{0}.Subtract({1})"  UniqueName="column"></telerik:GridCalculatedColumn>

If NOT, what programmatic approach or steps need to be taken?

thanks,

will
0
Shinu
Top achievements
Rank 2
answered on 09 Jun 2011, 07:59 AM
Hello William,

You can access the cell value (DateTime value) from code and find the difference using DateTime.Subtract method as mentioned in the above post. Hope this helps.

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
   {
       if (e.Item is GridDataItem)
       {
           GridDataItem item = (GridDataItem)e.Item;
           DateTime date1 =Convert.ToDateTime(item["DATE_EXPIRED"].Text);
           DateTime date2 = Convert.ToDateTime(item["TRIAL_REQUEST_DATE"].Text);
           System.TimeSpan diff1 = date2.Subtract(date1);
       }
   }

-Shinu.
0
William
Top achievements
Rank 1
answered on 09 Jun 2011, 04:43 PM
Hi Shinu, Thanks so much for the info!

So, let say there's a GRIDCALCULATEDCOLUMN column called "TRIAL_PERIOD".
I can use it to assign and show the value from within protectedvoidRadGrid1_ItemDataBound like this?

item["TRIAL_PERIOD"].Text = diff1.Days.toString();

will
0
William
Top achievements
Rank 1
answered on 10 Jun 2011, 01:02 AM
For those who followed this thread:

found this: http://www.telerik.com/community/forums/aspnet-ajax/grid/gridcalculatedcolumn-and-dataformatstring.aspx

tested (on my proj) to allow filter 'and' sorting as well, sweet!!

thanks,

will
Tags
Grid
Asked by
ketan reddy
Top achievements
Rank 1
Answers by
Sebastian
Telerik team
William
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Share this question
or