Solved

Calculate Time worked in Gridview

Posted on 2011-03-04
4
558 Views
Last Modified: 2013-11-07
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?
0
Comment
Question by:fishbowlstudios
4 Comments
 
LVL 10

Expert Comment

by:kdwood
ID: 35040356
fishbowl,

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

Keith
0
 

Author Comment

by:fishbowlstudios
ID: 35040398
DateTime Values come from a Database via a LINQ query. I assume they are coming from a DateTime datatype.
0
 
LVL 35

Accepted Solution

by:
Miguel Oz earned 500 total points
ID: 35040988
Yes you can use a template field, but I think you are better off adding this value to your LINQ collection, so that it comes directly on the binding.

If you proceed with template field (for whatever reason), then you need code similar to: (assumption your employee object contains start and enddate properties)
    protected void CustomerGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //This event is executed multiple times per row due to data bound issues.
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Employee item = e.Row.DataItem as Employee;
            DateTime newDT = item.endDate.Subtract(item.startDate);                      
            //Assign newDT to your template field.
        }
    }
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 35045982
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
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now