Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Calculate Time worked in Gridview

Posted on 2011-03-04
4
Medium Priority
?
568 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 51

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

688 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