none
Sum of time in datagridview column RRS feed

  • Question

  • Hello,

    I want to make a sum of time's of a datagridview column

    Use this for numbers , that works

    Dim sum = (From row As DataGridViewRow In DataGridView1.Rows.Cast(Of DataGridViewRow)()
                           Select CDec(row.Cells(15).Value)).Sum
    
                TextBox8.Text = sum.ToString

    But not for time.

    For example this is in the column

    14:55
    04:12
    00:19
    142:23
    00:08
    06:59
    
    How to i make a sum of all these time's in textbox 8 ? The column in nr 15 or Totaalgevaren

    Thursday, September 20, 2018 2:58 PM

All replies

  • Hello, 

    In the way you chose for solution - you may cast value of the TimeSpan - this will give proper Sum. Most likely you will have a new function to convert field value to to the TimeSpan.

    Another way - value in the field can be represented as Double type. It also supposed to give proper Sum. But Sum will be Double type and you need to convert it to DateTime if you expect min:sec format.

    P.S. I would not do calculation on the Grid.Cell value - it much simpler to do this on DataTable if you have DataTable as DataSource.


    Sincerely, Highly skilled coding monkey.


    Thursday, September 20, 2018 3:23 PM
  • how are you putting the time value there in the first place? In almost all cases where I am getting a timespan I write it in total seconds. This way you can use datatable expressions to get hour/min/whatever. Also your code would work as is.

    I also agree that you should be using the datatable and not the grid


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Thursday, September 20, 2018 3:38 PM
  • Thanks for the reply,

    I use this to calculate and the outcome i am saving in a database.

    Difference between 2 textboxes , what do i change to save it to minutes ?

    The outcome is in Totaalgevarenleeg.text . Can i save the total minutes in lets say textbox1 ? 

    When the outcome is in minutes i know how to sum form the database i think.

     Dim dt1 As DateTime = Nothing
                If DateTime.TryParse(Vertrektotaal.Text, dt1) Then
                End If
                Dim dt2 As DateTime = Nothing
                If DateTime.TryParse(Datumaankomsttotaal.Text, dt2) Then
                End If
                Dim diff As TimeSpan = dt2.Subtract(dt1)
                Totaalgevarenleeg.Text = String.Format("{0:00}", (diff.Days * 24 + diff.Hours)) & ":" & String.Format("{0:00}", diff.Minutes)

    Thursday, September 20, 2018 5:56 PM
  • Try just 

    Diff.TotalMinutes


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Thursday, September 20, 2018 8:29 PM
  •  

    Dim dt1 As DateTime = Nothing If DateTime.TryParse(Vertrektotaal.Text, dt1) Then End If Dim dt2 As DateTime = Nothing If DateTime.TryParse(Datumaankomsttotaal.Text, dt2) Then End If Dim diff As TimeSpan = dt2.Subtract(dt1)

    Totaalgevaren.Text = String.Format(diff.Minutes)


    is not working , it showing only the minutes after the hours.

    And not the total minutes.

    What String.Format do i use for the total amount of minutes , and not days hours and minutes

    Thursday, September 20, 2018 8:38 PM
  • Thanks Gtripodi,

    It works , what code do you use to calculate back to hours and minutes.

    Thursday, September 20, 2018 9:09 PM
  • To insert it into a textbox you could do Diff.TotalMinutes.ToString

    To answer your other question, post where you are filling your datatable


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Thursday, September 20, 2018 10:34 PM
  • >What String.Format do

    You need to read documentation for String.Format.

    If you do so, there will be a description of what need to be send as first argument,


    Sincerely, Highly skilled coding monkey.

    Friday, September 21, 2018 7:40 AM
  • It works , what code do you use to calculate back to hours and minutes.

    Try this:

    Dim totalMinutes = CInt(Math.Ceiling(Diff.TotalMinutes))
    Dim text = String.Format("{0:00}:{1:00}", totalMinutes \ 60, totalMinutes Mod 60)


    Then assign text to textbox.




    • Edited by Viorel_MVP Friday, September 21, 2018 7:46 AM
    Friday, September 21, 2018 7:45 AM
  • Thanks Viorel_

    I am saving the minutes to a database , make a sum of the total from the database directly and put it in textbox Totaalgevarenminuten ,

    And use this to get the total time in hours and minutes

     Dim totalMinutes = Totaalgevarenminuten.Text
            Dim text = String.Format("{0:00}:{1:00}", totalMinutes \ 60, totalMinutes Mod 60)
            Tijdtotaal.Text = text

    In the database

    2274 85 112 140 1197 total minutes 3808 Becomes 63:28



    Friday, September 21, 2018 9:53 AM
  • Are you good then? No more questions?

    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Friday, September 21, 2018 12:50 PM
  • Thanks for your help . Question closed

    Store in minutes was a good tip.

    Friday, September 21, 2018 1:07 PM
  • Hi,

    I am glad you have got your solution, we appreciated you shared us your solution and mark it as an answer.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, September 25, 2018 1:04 AM