Solved

Calculate Time worked in Gridview

Posted on 2011-03-04
4
564 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 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 50

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Build a string of emails from a gridview 2 31
How to set focus on a dynamic control 18 52
Windows Universal App 22 63
Manage big list of parameter list 8 25
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
Suggested Courses

751 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