Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Calculate Time worked in Gridview

Posted on 2011-03-04
4
Medium Priority
?
569 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 36

Accepted Solution

by:
Miguel Oz earned 2000 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 52

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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses

971 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