Link to home
Start Free TrialLog in
Avatar of fishbowlstudios
fishbowlstudiosFlag for United States of America

asked on

Calculate Time worked in Gridview

I am working on a program to calculate hours worked for an employee. How do I take two time values from two gridview columns and add them together? Should I use a template field?
Avatar of kdwood
kdwood

fishbowl,

Is the data in both columns expressed as hours or Date/Time values like a Start and End time?

Keith
Avatar of fishbowlstudios

ASKER

DateTime Values come from a Database via a LINQ query. I assume they are coming from a DateTime datatype.
ASKER CERTIFIED SOLUTION
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
TimeSpan it is:

            Employee item = e.Row.DataItem as Employee;
            TimeSpan hoursWork = item.endDate.Subtract(item.startDate);
            Console.WriteLine(hoursWork.TotalHours.ToString());                    
            //Assign hoursWork to your template field.

Or, if you rather have two timespans:

            Employee item = e.Row.DataItem as Employee;
            TimeSpan workHours = item.workPeriod1.Add(item.workPeriod2);
            Console.WriteLine(workHours.TotalHours.ToString());                    
            //Assign workHours to your template field.

/gustav