Click here to Skip to main content
15,893,190 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi guys,

I need to find the total and subtotal of datetime column in gridview.

iam using gridview helper( http://www.agrinei.com/gridviewhelper/gridviewhelper_en.htm[^] )

to find the solution.It is working for integer values

for datetime column it is displaying as string eg. 08:30:0005:30:00,I need the result

to be
1st row -> 08:30:00
2nd row -> 05:30:00

result  -> 14:00:00

can anyone please help me to find the solution.

Thanks in advance.
Posted
Updated 30-Sep-14 6:33am
v2
Comments
Sergey Alexandrovich Kryukov 30-Sep-14 12:36pm    
Solution for what? What would be the meaning of summing up points in time? I would understand if you sum up durations (time spans), it would make perfect sense.
—SA

1 solution

It is apparent that you obtained string concatenation, and not numeric calculation. Probably you just used '+' operator on strings.

No, please see my comment to the question. Summing up objects of the type System.DateTime makes no sense at all; they represents points on the time axis.

Consider using System.TimeSpan instead. But before calculating the sum, you need to parse the string to this type. Please see all the methods named Parse, ParseExact, TryParse and TryParseExact:
http://msdn.microsoft.com/en-us/library/system.timespan_methods%28v=vs.110%29.aspx[^].

—SA
 
Share this answer
 
v2
Comments
saisrid 30-Sep-14 13:57pm    
Sorry as iam new to dotnet, my way of queries may not be understandable will rectify in future.

As per your advice, I have tried using TimeSpan.Parse()

// changes done in code.

case "System.TimeSpan": return TimeSpan.Parse(a) + TimeSpan.Parse(b);

I have got compilation error as:

CS1502: The best overloaded method match for 'System.TimeSpan.Parse(string)' has some invalid arguments

Please advice.
Sergey Alexandrovich Kryukov 30-Sep-14 14:02pm    
Your "a" and "b" must be strings representing TimeSpan. Read about formats of TimeSpan represented by string. Just read starting the page I referenced.
Prefer TryParse, TryParseExact, if you can possibly expect parsing exceptions, to avoid them.
—SA

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