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

Time Calculation in Rad Grid

3 Answers 32 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Syed M Ali Shah
Top achievements
Rank 1
Syed M Ali Shah asked on 07 Sep 2009, 05:12 AM
Hi,

I am using a RadGrid. There is a griddatetimecolumn TotalTime represting time from sqlserver database. I have to generate a new column in the grid at run time by subtracting time in last column - 2nd last column.
Example:
S.NO.        Total_Time    DrivingTime
                    (HH:MM)         (HH:MM)
1                01:00            01:00
2                01:35            00:35
3                02:00            00:25

Now I have Total_time from DB, and want DrivingTime to be generated at runtime by doing simple calculations on Total_Time column.i.e. Just subtracting current row total_time with previous row total_time (if exist)  e.g. RowNo.3 DrivingTime= (02:00 - 01:35) = (00:25).

I am using C#

Any help will be highly appreciated.

Cheers, Ali

3 Answers, 1 is accepted

Sort by
0
Syed M Ali Shah
Top achievements
Rank 1
answered on 09 Sep 2009, 02:33 AM
Reply is awaited please

thanks

Cheers,
S
0
Princy
Top achievements
Rank 1
answered on 09 Sep 2009, 11:31 AM
Hello Syed,

Try out the following logic and see if it helps you achieve the required:
aspx:
<telerik:GridDateTimeColumn DataField="Date" DataFormatString="{0:HH:mm}"  UniqueName="DateTimeColumn" HeaderText="DateTimeColumn" >              
</telerik:GridDateTimeColumn> 
 
<telerik:GridTemplateColumn UniqueName="TemplateColumn"
     <ItemTemplate> 
           <asp:Label ID="dateLabel" runat="server"></asp:Label> 
     </ItemTemplate> 
</telerik:GridTemplateColumn> 

c#:
protected void RadGrid1_PreRender(object sender, EventArgs e) 
    { 
        for (int rowIndex = RadGrid1.Items.Count - 2; rowIndex >= 0; rowIndex--) 
        { 
            GridDataItem row = RadGrid1.Items[rowIndex]; 
            GridDataItem previousRow = RadGrid1.Items[rowIndex + 1]; 
            DateTime dt1 = Convert.ToDateTime(row["DateTimeColumn"].Text); 
            DateTime dt2 = Convert.ToDateTime(previousRow["DateTimeColumn"].Text); 
            TimeSpan ts = dt2.Subtract(dt1); 
            ((Label)previousRow.FindControl("dateLabel")).Text = ts.ToString(); 
            if (rowIndex == 0) 
            { 
                ((Label)row.FindControl("dateLabel")).Text = row["DateTimeColumn"].Text; 
            }                
        } 
    } 

Thanks
Princy.
0
Syed M Ali Shah
Top achievements
Rank 1
answered on 10 Sep 2009, 12:36 PM
Thanks for your detailed reply.
I am so surprised, why this is not working with my system. Does this thing work on your system? I think i am unable to get reference to label control by using FindControl. Could you please check and make a simple example for me for the above mentined scenario, just without any db connections, so that i may see where i am stuck? I will be very thankful.

Cheers,
S
Tags
Grid
Asked by
Syed M Ali Shah
Top achievements
Rank 1
Answers by
Syed M Ali Shah
Top achievements
Rank 1
Princy
Top achievements
Rank 1
Share this question
or