Click here to Skip to main content
15,907,913 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have three columns in my gridview.
The first and second column has a time format . example : 19:00 | 21: 00
and on the third column should be the total time in the first and second column.

I tried this:

VB
For i As Integer = 0 To gridview.Rows.Count - 1
               gridview.Rows(i).Cells(3).Text = gridview.Rows(i).Cells(0).ToString - gridview.Rows(i).Cells(1).ToString
           Next


but it is not working.
Can anyone give me other way?
Thanks !
Posted
Comments

In RowDataBound [^]event calculate the difference for each row

OR

Return the difference from your dataSource (database?) and simply bind the column to existing difference

OR

Use jQuery

OR

Add template field calling static (or protected) function from your server side code calculating the value
(example:
XML
<asp:TemplateField HeaderText="Total" ItemStyle-HorizontalAlign="Center" >
   <ItemTemplate>
      <%# GetDifference((Eval("time2"), Eval("time1")) %>
   </ItemTemplate>
</asp:TemplateField>

)


In all the cases you have to use DateDiff function (exists in SQL Server, there must be equivalent in other databases). Also exists in .net... pick your solution.
 
Share this answer
 
v4
Comments
NekoNao 29-Sep-14 3:41am    
Thanks ! I did the "Return the difference from your dataSource (database?) and simply bind the column to existing difference"

But i have a problem.

18:00 - 19:30 = should be "1.30" right? but in databind , its only 1. What should i do?
Sinisa Hajnal 29-Sep-14 3:45am    
Are you receiving errors? And no: it should be 1.5 :) 1.30 would be 1 hour and 20 minutes...

I would say you need to use minutes in datediff and divide with 60 - datediff counts whole (integer) steps of your time unit (and 19:30 - 18:00 equals in hours 19 - 18)
Tiwari Avinash 29-Sep-14 5:24am    
select right('00'+convert(varchar,datediff(HOUR,CONVERT(datetime,'18:00',103),CONVERT(datetime,'19:30',103))),2)
+':'+right('00'+
convert(varchar,datediff(MINUTE,CONVERT(datetime,'18:00',103),CONVERT(datetime,'17:30',103))),2)


if it helps please mark it as answer
You can do it easily from SQL DB as

SQL
select right('00'+convert(varchar,datediff(HOUR,CONVERT(datetime,'18:00',103),CONVERT(datetime,'19:30',103))),2)
+':'+right('00'+
convert(varchar,datediff(MINUTE,CONVERT(datetime,'18:00',103),CONVERT(datetime,'17:30',103))),2)

if it helps please mark it as answer
 
Share this answer
 
Okay. I found the solution here. And it was so easy . TIME DIFFERENCE IN MYSQL[^]
 
Share this answer
 
Comments
Sinisa Hajnal 29-Sep-14 3:52am    
And you couldn't have used that before asking the question because...?
NekoNao 29-Sep-14 4:57am    
Because I haven't got any idea at all. but thanks to you . i got an idea. I didnt think of using query at first but then you said "Return the difference from your dataSource (database?) and simply bind the column to existing difference".
Sinisa Hajnal 29-Sep-14 6:26am    
Well, glad to be of help :)
Please try this,

C#
protected void gvGridTime_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DateTime dateFrom = Convert.ToDateTime(e.Row.Cells[1].Text);
                DateTime dateTo = Convert.ToDateTime(e.Row.Cells[2].Text);
                var diff = dateTo.Subtract(dateFrom);
                e.Row.Cells[3].Text = Convert.ToDateTime(diff.ToString()).ToString("hh:mm:ss");
            }            
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900