Add hours in vb?

1

I have a DatagridView with hours of the form hh:mm:ss as I could add all those hours. I have the following but it does not work for me, add the time as a string. For example, 9: 22: 347: 24: 14

Dim horasT
For Each row In Datos.Rows
   If row.Cells(6).Value.ToString() <> "0" Then
      horasT += CDate(row.Cells(6).Value), "hh:mm:ss")
   End If
Next
    
asked by Alcides Salazar 20.04.2017 в 16:11
source

3 answers

1

I found this that maybe it can serve you:

 Dim Horas As Integer = 0
    Dim Minutos As Integer = 0

    For Each row As DataGridViewRow In DataGridView1.Rows
        If Not row.IsNewRow Then
            Horas = Horas + Convert.ToDateTime(row.Cells("HORAS").Value).Hour
            Minutos = Minutos + Convert.ToDateTime(row.Cells("HORAS").Value).Minute
        End If
    Next

    txtResultado.Text = String.Format("{0}:{1}", Horas + Fix(Minutos / 60), Minutos Mod 60)
    
answered by 20.04.2017 / 17:42
source
1

Good morning ..

You have a couple of problems with the code. You are not defining the data type of times T, therefore the compiler infers it, and for what you are saying it infers it as text (I would not know why).

Also, you are using the old CDate function, instead of using a datetime or timespan object, or whatever you like to handle this.

Those Objects have an add method, which is used to add another object of the same type to them.

Look which is the one that suits you according to your case.

    
answered by 20.04.2017 в 16:26
0

Alcides Salazar. Because you only want to add the time, and your column in your DataGridView where is the time with format hh: mm: ss, I have solved it with the following code that I show you next:

Dim horasTotal As TimeSpan
For Each registro As DataGridViewRow In DataGridView1.Rows
    horasTotal += TimeSpan.Parse(registro.Cells("txtFecha").Value)
Next

Label1.Text = horasTotal.ToString

The key part of the code is in saving and adding the time as TimeSpan and not in Date .

  • I have taken the liberty of making a video on YouTube showing this solution recreating your example, which you can see in this link
answered by 20.04.2017 в 20:12